How to get the current page count?

I have to create a PDF document from a grid view. When I try to count the total number of pages, I don't get the current amount.

Dim pdfDoc As New Document(PageSize.A2, 50, 50, 50, 50) ................... pdfDoc.PageCount

Posted on StackOverflow on Jul 21, 2014 by Please help me

Your error is based on a misunderstanding of the basic concepts of iTextSharp.

A document is created in 5 steps:

  1. Create a document. This document doesn't know anything about the presentation of the document, only about its content.
  2. Create a writer. You are creating a PdfWriter that will translate the content into a presentation, more specifically into a PDF document with one or more pages.
  3. Open the document.
  4. Add content.
  5. Close the document.

You are asking the document object for the current page number, yet the document isn't aware of its presentation. It doesn't even know that a PDF is produced.

You should ask the writer that is responsible for creating the PDF how many pages were already created; writer.PageNumber will return that number.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now