PDF generation and pagination with PDFNet

Q:

We are using the Visual Studio and C# for the development of our WPF application. We have the problem, that we try to print a User Control to PDF. The User Control can extend over more than one page. How can we perform the pagination ? And secondly, each page of the multi-page PDF should have a fixed header/footer. How can this be accomplished with PDFNet components ?

A:

There are multiple ways to create new PDF content using PDFNet. These are described in the following article:

https://groups.google.com/d/topic/pdfnet-sdk/lhM9u-FwSZ0/discussion

Since you are programming in .NET, the simplest way to create dynamic PDF content using Xaml2Pdf: (http://www.pdftron.com/pdfnet/samplecode.html#Xaml2Pdf)

The same code can be adopted to serialize other WPF content including custom User Controls to PDF. For an example please see:

https://groups.google.com/d/topic/pdfnet-sdk/VJoV0Hd_bzI/discussion

Another option is dynamic conversion from HTML (string or file) to PDF via pdftron.PDF.Html2Pdf (http://www.pdftron.com/pdfnet/html/classpdftron_1_1PDF_1_1HTML2PDF.html)

In case you need low-level control you can use ElementBuilder/Writer (https://groups.google.com/d/topic/pdfnet-sdk/bSYgR77JiAw/discussion)


In case you go down WPF to PDF (i.e. Xaml2Pdf route, http://www.pdftron.com/pdfnet/samplecode/Xaml2PdfTestCS.cs), the pagination is controlled using .NET API (such as IDocumentPaginatorSource - http://msdn.microsoft.com/en-us/library/ms522721(v=vs.110).aspx)

Pagination in Xaml2Pdf is implemented via DocumentPaginatorWrapper (http://www.pdftron.com/pdfnet/samplecode/Pdftron.Xaml.Convert.cs) which is derived from DocumentPaginator (http://msdn.microsoft.com/en-us/library/system.windows.documents.documentpaginator.aspx). Alternatively you could derive from FlowDocumentPaginator.

As a starting point to get familiar with WPF DocumentPaginators please take a look at the following resources:

http://www.switchonthecode.com/tutorials/wpf-printing-part-2-pagination

http://janrep.blog.codeplant.net/post/WPF-Multipage-Reports-Part-IV-Pagination.aspx

http://www.codeproject.com/Articles/31834/FlowDocument-pagination-with-repeating-page-header

http://www.codeproject.com/kb/wpf/pimpeddocumentpaginator.aspx

http://blogs.msdn.com/b/fyuan/archive/2007/03/10/convert-xaml-flow-document-to-xps-with-style-multiple-page-page-size-header-margin.aspx

http://www.codeproject.com/Articles/164033/WPF-Visual-Print-Component

http://stackoverflow.com/questions/3033864/defining-where-on-the-page-the-flowdocument-i-am-printing-will-start-and-end