I am creating a pdf document. There is a PdfPTable in which some rows that will repeated in every page. What I want is to add page number in that table cell on each page.
I tried by rendering cell in PdfPageEventHelper and write table in onEndPage() but page number count is not increased and is always 1 in very page .
How can I achieve this?
[Updated]
I rendered table OnStartPage() instead of constructor. I used writer.PageNumber for pageCount. Page number is increased now but table is appeared again and again in every page.
public class itsEventsHandler : PdfPageEventHelper
{
int pageCount;
protected PdfPTable tblAccInfo = new PdfPTable(3);
public itsEventsHandler()
{
}
public override void OnStartPage(PdfWriter writer, Document document)
{
pageCount = writer.PageNumber;
this.BuildBorderCell(tblAccInfo, "A/C No.", boldFont);
this.BuildBorderCell(tblAccInfo, "External Doc No.", boldFont);
this.BuildBorderCell(tblAccInfo, "PAGE", boldFont);
this.BuildBorderCell(tblAccInfo, accountNo, titleFont);
this.BuildBorderCell(tblAccInfo, docNo, titleFont);
this.BuildBorderCell(tblAccInfo, pageCount.ToString(), titleFont);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
tblAccInfo.WriteSelectedRows(0, -1, document.LeftMargin + 53f,
document.PageSize.Height - 250f, writer.DirectContent);
}
}
I expect the page number is in table cell on every page.
Instead of using the constructor you should override
onStartPage( https://itextsupport.com/apidocs/iText5/5.5.9/com/itextpdf/text/pdf/PdfPageEventHelper.html#onStartPage-com.itextpdf.text.pdf.PdfWriter-com.itextpdf.text.Document- )There is a parameter
documentfrom type Document with a functiongetPageNumber(https://itextsupport.com/apidocs/iText5/5.5.9/com/itextpdf/text/Document.html#getPageNumber-- )Edit
After reading links of @mkl I have to withdraw the above. Real answer is from @mkl in comment.