0

I wanted to get the page number in each page of the pdf generated using iTextSharp 5.0.2.0.

right now part of the code is

e.FooterText = writer.CurrentPageNumber.ToString();

But when i do this , it is showing the page number but the problem is it is showing same page number in all the pages. like "1" in all the pages. how to get the page number for the pdf file using iTextSharp version 5.0.2.0 ??Any ideas ??

CodeRed
  • 905
  • 1
  • 6
  • 24
  • 1
    You should add a better code sample of your problem, one that could help others locate the problem. Have you inherited from `PdfPageEventHelper` and coded `OnEndPage` event? Check this [related or duplicated question](https://stackoverflow.com/questions/18996323/add-header-and-footer-for-pdf-using-itextsharp) – Cleptus Sep 20 '19 at 07:30

2 Answers2

0

You'll have to open the PDF with iTextSharp and follow the below code will help you


public void AddPageNumberToPDF(string physicalDocPath, bool showPageOfPage)
{
   byte[] Fbytes = File.ReadAllBytes(physicalDocPath);
   PdfReader reader = new PdfReader(Fbytes);
   int n = reader.NumberOfPages;
   using (var fileStream = new FileStream(physicalDocPath, FileMode.Create, FileAccess.Write))
   {
       var document = new Document(reader.GetPageSizeWithRotation(1));
       var writer = PdfWriter.GetInstance(document, fileStream);
       document.Open();
       PdfContentByte cb = writer.DirectContent;
       int p = 0;

       for (int page = 1; page <= reader.NumberOfPages; page++)
       {
           document.NewPage();
           p++;

           PdfImportedPage importedPage = writer.GetImportedPage(reader, page);
           cb.AddTemplate(importedPage, 0, 0);

           BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
           cb.BeginText();
           cb.SetFontAndSize(bf, 10);
           if (showPageOfPage)
           {
               cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, p.ToString()+"/"+n.ToString(), 575, 17, 0);
           }
           else
           {
               cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, p.ToString(), 575, 17, 0);
           }
           cb.EndText();
       }

       document.Close();
       writer.Close();
   }
}
Palle Due
  • 5,929
  • 4
  • 17
  • 32
Kinjal Gor
  • 401
  • 2
  • 5
  • 15
  • This is *one* option, the OP does not *have to open the PDF with iTextSharp*, he can alternatively add correct page numbers already during the creation of the pdf as proposed by bradbury9 in his comment to the question. – mkl Sep 20 '19 at 07:47
0

As stated by @bradbury9, an OnEndPage event is what you're aiming for.

Here's an example of the code that will add the desired footer text in the desired page position every time the OnPageEnd event is triggered

public class MyPdfPageEventHandler: PdfPageEventHelper
{
    const float horizontalPosition = 0.5f;  // %50 of the page width, starting from the left
    const float verticalPosition = 0.1f;    // %10 of the page height starting from the bottom

    public override void OnEndPage(PdfWriter writer, Document document)
    {
        var footerText = new Phrase(writer.CurrentPageNumber.ToString());

        float posX = writer.PageSize.Width * horizontalPosition;
        float posY = writer.PageSize.Height * verticalPosition;
        float rotation = 0;

        ColumnText.ShowTextAligned(writer.DirectContent, Element.PHRASE, footerText, posX, posY, rotation);
    }
}

And here's some sample code on how to make it work

static void Main(string[] args)
{
    FileStream fs = new FileStream("NewDocument.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
    Document doc = new Document();
    PdfWriter writer = PdfWriter.GetInstance(doc, fs);
    writer.PageEvent = new MyPdfPageEventHandler(); //This will trigger the code above

    doc.Open();
    doc.Add(new Paragraph("First Page"));
    doc.NewPage();
    doc.Add(new Paragraph("Second Page"));
    doc.NewPage();
    doc.Add(new Paragraph("Thid Page"));
    doc.Close();
}

You can use the MyPdfPageEventHandler class to override other page events such as OnStartPage and such.

Innat3
  • 3,561
  • 2
  • 11
  • 29