Click here to Skip to main content
14,870,596 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

How do I get the total page count of a document of the pdf using itextsharp?

I want to display total pagecount on footer of each page.
There is the property document.PageCount (but from this we can just set to count as user want).
From the following coding I got total count in OnCloseDocument() event of PdfPageEventHelper but not getting this count on OnEndPage().

C#
int totcountPage = 0;
public override void OnCloseDocument(PdfWriter writer, Document document)
{
       total.BeginText();
       int pageNumber = writer.PageNumber - 1;
       total.ShowText(Convert.ToString(pageNumber));
       total.EndText();
       totcountPage = writer.PageNumber;
    //MessageBox.Show(p.ToString());        
} 


In above I have got total count of pages but I want to display on footer of page.
C#
int i=0;
public override void OnEndPage(PdfWriter writer, Document doc)
{  
   Paragraph para = new Paragraph(i + " of "  + totcountPage + "", footer);
  doc.Add(para);
}


Here as event OnEndPage() called first as before of OnCloseDocument() , thats why displaying totcountPage as 0. so I am not getting right output which I want.

For ex showing output
1 of 0... On first page
5 of 0...On Fifth Page

For ex expected output
1 of 5... On first page
5 of 5 ...On Fifth Page

So please help me.
Posted
Updated 31-Mar-21 1:25am
v2
Comments
Dalek Dave 29-Sep-11 3:49am
   
Edited for Code Blocks, Grammar, Spelling and Readability.

The way I see it, you'll have 2 options:

1) Build your complete PDF and then add your footers, but I don't know if that's possible with ItextSharp.

2) Create a memorystream, build your PDF, get the total number of pages and then create a filestream that (re)builds your PDF with the correct footnotes.
   
I have no idea. I think it is not possible .Because until the document is finished that can you know how many pages you will get .Understand?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900