Your error is based on a misunderstanding of the basic concepts of iTextSharp.
A document is created in 5 steps:
- Create a document. This document doesn't know anything about the presentation of the document, only about its content.
- Create a writer. You are creating a
PdfWriterthat will translate the content into a presentation, more specifically into a PDF document with one or more pages.
- Open the document.
- Add content.
- 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.