mkautzm mkautzm - 2 months ago 12
C# Question

PDFsharp: Is there a way to generate "Page X of Y" in the header of the page?

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.


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 and paragraph.AddNumPagesField() for the total page count.

Sample that uses AddPageField

Code snippet from the sample:

// Create a paragraph with centered page number. See definition of style "Footer".
Paragraph paragraph = new Paragraph();

// Add paragraph to footer for odd pages.
// Add clone of paragraph to footer for odd pages. Cloning is necessary because an object must
// not belong to more than one other object. If you forget cloning an exception is thrown.

Code snippet that sets the tab stop (assuming DIN A 4 with a body with of 16 cm):

style = document.Styles[StyleNames.Footer]; 
style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center); 

Both snippets taken from the linked site. Sample code is also available for download.