Modify the content of pdf based on header

Hello,

Currently what we are doing:
We have our data in HTML format. So we are generating pdf from html. We are using Converter.ConvertHTML from Aspose.HTML. Also, we have pdf headers and footers in HTML form. What we are doing is, we are keeping top and bottom margin of fixed height and then generating pdf, once we have pdf then we are applying header and footer to it. Everything is working fine.

What we want now
Now, we have a new ask such that each page might has different header and footer, which will be in HTML form and can have different height. Consdiering above logic, how can I apply diferent header and footer, considering, I want to apply fixed height for header and footer for each page. I have my logic to check height from HTML, but i cant find ways to apply different margin to each page.

It will be great help if you can help me.

@prashantm1989

To apply different headers and footers with varying heights for each page in your PDF, follow these steps:

  1. Generate the PDF from HTML: Continue using your existing method to convert HTML to PDF, ensuring you can determine the height of each header and footer from your HTML content.

  2. Set Margins Dynamically: After generating the PDF, iterate through each page and set the margins based on the height of the header and footer for that specific page. This allows you to adjust the content area dynamically.

  3. Add Headers and Footers: Use the Aspose.PDF library to add headers and footers to each page. Create a HeaderFooter object for each page and set its margins accordingly.

Here’s a code snippet to illustrate how you can achieve this:

using Aspose.Pdf;

public void AddDynamicHeadersAndFooters(string inputPdfPath, string outputPdfPath, List<string> headers, List<string> footers, List<double> headerHeights, List<double> footerHeights)
{
    // Open the existing PDF document
    Document document = new Document(inputPdfPath);

    for (int i = 1; i <= document.Pages.Count; i++)
    {
        // Create header and footer
        HeaderFooter header = new HeaderFooter();
        HeaderFooter footer = new HeaderFooter();

        // Set header content
        header.Paragraphs.Add(new TextFragment(headers[i - 1]));
        header.Margin.Top = headerHeights[i - 1];

        // Set footer content
        footer.Paragraphs.Add(new TextFragment(footers[i - 1]));
        footer.Margin.Bottom = footerHeights[i - 1];

        // Bind the header and footer to the page
        document.Pages[i].Header = header;
        document.Pages[i].Footer = footer;

        // Adjust the content area based on header and footer heights
        document.Pages[i].Margin = new MarginInfo
        {
            Top = headerHeights[i - 1],
            Bottom = footerHeights[i - 1]
        };
    }

    // Save the updated PDF document
    document.Save(outputPdfPath);
}

In this example, you will pass the paths for the input and output PDFs, along with lists of headers, footers, and their respective heights. This ensures that each page can have its own unique header and footer without overlapping content.

If you have any further questions or need additional assistance, please let me know!

Hello,

I tried this code. To keep it simple I tried only for headers first. This code does not move content of page a but lower, instead it applies same margin for all pages though I set it differently. Here is my code with pdf files.

using System;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using Aspose.Pdf.Text;
using Aspose.Pdf;

class Program
{
    static void Main(string[] args)
    {   
        string path = @"C:\Users\prashant.mahajan\Downloads\";
        string dataDir = path + "document (3).pdf";
        List<string> headers = ["<p>This is hello world for page1.</p>", "<p>This is hello world for page2.</p>", "<p>This is hello world for page3.</p>"];
        AddDynamicHeadersAndFooters(dataDir, path, headers, null, [100, 200, 500], null);


        void AddDynamicHeadersAndFooters(string inputPdfPath, string outputPdfPath, List<string> headers, List<string> footers, List<double> headerHeights, List<double> footerHeights)
        {
            // Open the existing PDF document
            Document document = new Document(inputPdfPath);

            for (int i = 1; i <= document.Pages.Count; i++)
            {
                // Create header and footer
                HeaderFooter header = new HeaderFooter();
                HeaderFooter footer = new HeaderFooter();

                // Set header content
                header.Paragraphs.Add(new TextFragment(headers[i - 1]));
                header.Margin.Top = 0;// headerHeights[i - 1];

                // Set footer content
                if (footers != null)
                {
                    footer.Paragraphs.Add(new TextFragment(footers[i - 1]));
                    footer.Margin.Bottom = footerHeights[i - 1];
                }

                // Bind the header and footer to the page
                document.Pages[i].Header = header;
                document.Pages[i].Footer = footer;

                // Adjust the content area based on header and footer heights
                if (headerHeights != null)
                {
                    document.Pages[i].PageInfo.Margin = new MarginInfo
                    {
                        Top = headerHeights[i - 1]
                    };
                }

                if (footerHeights != null)
                {
                    document.Pages[i].PageInfo.Margin = new MarginInfo
                    {                        
                        Bottom = footerHeights[i - 1]
                    };
                }
            }

            // Save the updated PDF document
            document.Save(outputPdfPath + "output.pdf");
        }
    }
}

Output.pdf (249.1 KB)

document (3).pdf (223.2 KB)

@prashantm1989

Would you kindly also share the sample HTML from which the PDF was generated in the first place? It will help us in investigating the issue accordingly. You can add the HTML to an archive and attach here with your message.

You can find the HTML in attached file.
Sample.docx (14.1 KB)

@prashantm1989

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-60462

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.