dev 0

dev 0

  • NA
  • 31
  • 36k

How to get total page count of pdf pages on footer on each page using Itextsharp

Oct 3 2011 7:41 AM
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().
 
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.
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.

Answers (1)