0

I have the following situation to handle using iTextSharp: I am creating a PDF report and I don't know previously the number of the pages.

In the footer of each page I insert the page number as: "1/x" where x is the total pages number of my report.

At this time I have a class named PdfHeaderFooter that extends PdfPageEventHelper in which I implement the OnEndPage() to create the footer into my PDF.

    // write on end of each page
    public override void OnEndPage(PdfWriter writer, Document document)
    {
        base.OnEndPage(writer, document);
        PdfPTable tabFoot = new PdfPTable(new float[] { 1F });
        tabFoot.TotalWidth = document.Right - document.Left;

        tabFoot.DefaultCell.Border = PdfPCell.NO_BORDER;
        tabFoot.DefaultCell.CellEvent = new RoundedBorder();

        PdfPTable innerTable = new PdfPTable(2);
        innerTable.SetWidths(new int[] { 247, 246 });
        innerTable.TotalWidth = document.Right - document.Left;  
        innerTable.DefaultCell.Border = PdfPCell.NO_BORDER;
        PdfPCell innerCellLeft = new PdfPCell(new Phrase("Early Warning - Bollettino")) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_LEFT };
        PdfPCell innerCellRight = new PdfPCell(new Phrase("Pag. " + numPagina + "/5")) { Border = PdfPCell.NO_BORDER, Padding = 5, MinimumHeight = 20, HorizontalAlignment = Element.ALIGN_RIGHT };
        innerTable.AddCell(innerCellLeft);
        innerTable.AddCell(innerCellRight);

        tabFoot.AddCell(innerTable);

        tabFoot.WriteSelectedRows(0, -1, document.Left, document.Bottom, writer.DirectContent);

        numPagina++;
    }

As you can see at this time I handle the situation using:

 PdfPCell innerCellRight = new PdfPCell(new Phrase("Pag. " + numPagina + "/5")) 

But in this way the total page number is fiexed (/5) but I can't previously know it the pages are 5 or a different number.

So I am thinking that I can handle the situation in the following way:

1) I can count the NewPage() call in the class that generate

2) Then I can try to modify the value of the total page int the footer of each page

But I don't know if this is a smart solution or if it is possibile to do

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

0

At the moment you're adding the page numbers (e.g. page X of Y), you know the value of X, but there's no way to know the value of Y.

There are two options to solve this. You can find this in the official documentation when you look at the Page X of Y keyword page.

One option is to introduce an empty PdfTemplate where you would normally have the Y value. You can then implement the onCloseDocument() event. This event is triggered right before the document is closed. At this moment, the value of Y is known and you can draw it to the PdfTemplate.

The other option is to create the PDF in two passes. You first create it in memory without "Page X of Y". Then you use PdfStamper to add the correct Page X of Y values (because you know the total number of pages from the first pass).

P.S. if you need code samples, you can find the C# ports of the Java examples here.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165