Skip to main content
Skip table of contents

How to get the current page count?

When I try to count the total number of pages, I don't get the current amount. Why is that?

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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.