Adding page number in header cell in every page

350 views Asked by At

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.

2

There are 2 answers

6
Mokuyobi On

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 document from type Document with a function getPageNumber (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.

1
Zan On

This is my solution.

 public class itsEventsHandler : PdfPageEventHelper
 {
   int pageCount;
   protected PdfPTable tblAccInfo = new PdfPTable(3);
   public itsEventsHandler()
   {

   }

   public void BuildBorderCell(PdfPTable pdfTable, string strText, 
      iTextSharp.text.Font font)
   {
        PdfPCell cell = new PdfPCell(new Phrase(strText, font));
        cell.Border = iTextSharp.text.Rectangle.TOP_BORDER;
        cell.PaddingTop = 5f;
        pdfTable.AddCell(cell);
   }


   public override void OnEndPage(PdfWriter writer, Document document)
   {
     pageCount = writer.PageNumber;

     //Creating the table
     PdfPTable tblAccInfo = new PdfPTable(3);
     tblAccInfo.TotalWidth = 450f;

     float[] accInfoWidths = new float[] { 50f, 50f, 50f};
     tblAccInfo.SetWidths(accInfoWidths);

     //Building table cell
     BuildBorderCell(tblAccInfo, "A/C No.", boldFont);
     BuildBorderCell(tblAccInfo, "External Doc No.", boldFont);
     BuildBorderCell(tblAccInfo, "PAGE", boldFont);

     BuildBorderCell(tblAccInfo, accountNo, titleFont);
     BuildBorderCell(tblAccInfo, docNo, titleFont);
     BuildBorderCell(tblAccInfo, pageCount.ToString(), titleFont);

     //Writing the table
     tblAccInfo.WriteSelectedRows(0, -1, document.LeftMargin + 53f, 
     document.PageSize.Height - 250f, writer.DirectContent);

     //Droping the table
     tblAccInfo = null;
  }     

}