It seems rather simple, but I can't find something like getPageCount() in the API. I can get it to return the current page, but not the total number of pages. Perhaps I'm missing it?
I would like to somehow be able to print 'Page 1 of 9' at the top of every page, where '1' of course is the current page number.
I know this question is old and has an accepted answer, however the question comes up among the first when searching for a PDFsharp solution.
For the record, achieving this in PDFsharp is easy. The
PdfDocument
class, found under thePdfSharp.Pdf
namespace contains a collection of pages (PdfDocument.Pages
). All you have to do is iterate through the collection and add the page counter somewhere on every page, using aXGraphics
object, that you can instantiate usingXGraphics.FromPdfPage(PdfPage)
.It's worth noting that if a XGraphics object already exists for a given page, before creating a new one, the old one needs to be disposed. This would fail:
Make sure to include the
using MigraDoc.DocumentObjectModel;
statement in your class.With PDFsharp it's up to you.
I presume you are using MigraDoc: With MigraDoc you can add a page header. Add
paragraph.AddPageField()
for the current page number andparagraph.AddNumPagesField()
for the total page count.Sample that uses AddPageField
Code snippet from the sample:
Code snippet that sets the tab stop (assuming DIN A 4 with a body with of 16 cm):
Both snippets taken from the linked site. Sample code is also available for download.