2

I am building a PDF at runtime in itext7.pdfHTML, using a template file. I want to add a footer to every page of the resulting PDF, which has two pages, but for some reason the footer only appears on the second page.

I'm trying to build on this example from the iText website. The example deals with page numbers, but since I'm just adding static text to my document, the principle is the same. Here's my code:

string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";

//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
    destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");

    ConverterProperties properties = new ConverterProperties();
    properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));

    HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);

    addFooter(id);
}

//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
    string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
    Document doc = new Document(pdfDoc);

    Paragraph foot = new Paragraph(footer);
    foot.SetFontSize(8);

    float x = 300; //559
    float y = 0; //806

    int numberOfPages = pdfDoc.GetNumberOfPages();
    for (int i = 1; i <= numberOfPages; i++)
    {
        doc.ShowTextAligned(foot, x, y, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
    }

    doc.Close();

    //delete temporary PDF
    File.Delete(destination);
}

I've tried setting i to 0 in the addFooter() "for" loop, but that doesn't solve the issue. How can I get the footer to appear on every page?

bmurrell30
  • 565
  • 8
  • 23
  • I can't see where you're looping through the pages. It looks like you're just adding a footer to the entire document & it makes sense why it would be on the bottom of the last page. – Mikael Jul 10 '20 at 17:42
  • @Mikael thanks for the reply. The loop is happening in the addFooter() function, starting with the creation of the "numberOfPages" int. This is fed from "pdfDoc.GetNumberOfPages()", which correctly returns 2. This means that the "doc.ShowTextAligned" function should be running twice, yet I only see the one footer. Does that make more sense? – bmurrell30 Jul 10 '20 at 17:46
  • Yes, the loop does happen but you're not calling the specific page number to do anything, so it's just performing the same action over and over until the loop is finished. – Mikael Jul 10 '20 at 17:48

1 Answers1

2

Yeah, you weren't specifying which page to add the footer to, so it only added it to the bottom of the entire document. Try this:

Note, the only change was: doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);

string footer = "This is the footer".
string body = "<span>This is raw HTML</span>";

//create a temporary PDF with the raw HTML, made from my template and given data
private void createPDF()
{
    destination = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/pdf_repo"), "tempFile.pdf");

    ConverterProperties properties = new ConverterProperties();
    properties.SetBaseUri(HttpContext.Current.Server.MapPath("~/templates/"));

    HtmlConverter.ConvertToPdf(body, new FileStream(destination, FileMode.Create), properties);

    addFooter(id);
}

//modify the PDF file created above by adding the footer
private void addFooter(string id)
{
    string newFile = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("pdf_repo", "finalFile.pdf");

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(destination), new PdfWriter(newFile));
    Document doc = new Document(pdfDoc);

    Paragraph foot = new Paragraph(footer);
    foot.SetFontSize(8);

    float x = 300; //559
    float y = 0; //806

    int numberOfPages = pdfDoc.GetNumberOfPages();
    for (int i = 1; i <= numberOfPages; i++)
    {
        doc.ShowTextAligned(foot, x, y, i, TextAlignment.CENTER, VerticalAlignment.BOTTOM);
    }

    doc.Close();

    //delete temporary PDF
    File.Delete(destination);
}
Mikael
  • 1,002
  • 1
  • 11
  • 22