Using PDFNet to convert PDF to WPF FixedPage

Q:

I would like to switch from TallComponents to PDFTron due to speed and stability issues I encountered with the former. Currently I use a method called ConvertToWpf(renderSettings, convertOptions) to convert a PDF page to WPF Fixed page:

FixedPage fixedPage = currentPage.ConvertToWpf(renderSettings, convertOptions);

I am then able to display and then Rasterize the FrameworkElement (using RenderTargetBitmap) using transforms and scaling to crazy resolutions such as 119x617.

How would I convert a PDF doc/page to FixedDoc/Page using PDFTron PDFNet?

A: You can generate a FixedDocument (i.e. an XPS file) using pdftron.PDF.Convert.ToXps(). Similarly you can convert a FixedDocument, a FixedPage, XAML, or any WPF UI element to PDF as shown in Xaml2Pdf sample (http://www.pdftron.com/pdfnet/samplecode/Xaml2PdfTestCS.cs).

We could easily wrap the current API to return a FixedPage directly, but before going down this route I just want to clarify why do you need to go through XAML/XPS/FixedPage strep? Rasterizing PDF content directly (e.g. using PDFDraw - http://www.pdftron.com/pdfnet/samplecode/PDFDrawTest.cs) will be always more efficient than translating to another representation, than rendering. Furthermore, the conversion process may introduce the errors/issues that are not present when rendering directly from PDF (e.g. try converting some PDF with transparency; e.g. http://www.pdftron.com/pub/TRANSAPRENCY.pdf). This type of files will fail to convert in most other converters. PDFTron will convert the file accurately but some elements (e.g. paths, text etc) may be rasterized. This can’t be avoided because WPF/XPS does not support all PDF graphics capabilities.

then Rasterize the FrameworkElement (using RenderTargetBitmap) using transforms and scaling to crazy resolutions such as 119x617.

You can achieve the same type of stretching using SetImageSize() with PDFDraw/PDFRasterizer .

For example:

using (PDFDraw draw = new PDFDraw()) {

using (PDFDoc doc = new PDFDoc(“my.pdf”)) {

draw.SetImageSize (119, 617, false);

draw.Export(doc.GetPage(1), “my.png”);

}

}

The last parameter in SetImageSize(http://www.pdftron.com/pdfnet/html/classpdftron_1_1PDF_1_1PDFDraw.html#186e9d4bfc092094e3482d400724e9ab), tells PDFDraw not to preserve aspect ratio (i.e. the image will be stretched to fit the output image).