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