WPF to XPS to PDF

Alex Hope O'Connor picture Alex Hope O'Connor · Dec 12, 2012 · Viewed 9.9k times · Source

I have implemented a report in standard WPF controls and have also implemented a DocumentPaginator to take these controls and convert them into a document for printing.

I have also implemented some code which uses the document paginator to render the pages to images and write them out to a PDF using PDFSharp, however this does not allow for copying and pasting, also the image quality is questionable.

I have experimented with the GhostXPS utility and was thinking of using it by saving out to an XPS document and then using GhostXPS to convert it to a PDF, this was promising, however the current version contains a bug that renders data copied from the generated PDF useless...

So I thought I would ask here to try and find a efficient method for doing this, Can someone please suggest a good way to take a DocumentPaginator and either save it directly out to a PDF, with copy/paste functionality or convert it from an XPS document to a PDF with this functionality?

Thanks, Alex.

EDIT - PDFSharp fix: Ok as requested here is what I did to compile the fix for the PDFSharp 1.31 XPS converter:

1) I downloaded the source from here:

PDFSharp - SourceForge

2) I followed the instruction in this post:

PSFSharp Fix

XpsParser.ImageBrush.cs (ln 22, added): brush.Opacity = 1;
PdfContentWriter.cs (ln 526, changed): if (opacity <= 1)

3) Then just compile the source, first open the 'PdfSharp-WPF.csproj' and build that, then open the 'PdfSharp.Xps.csproj' and build it as well, the relevent libraries are 'PdfSharp-WPF.dll' and 'PdfSharp.Xps.dll'

Note upon trying to build the source you will likely receive a few errors depending on which version of .NET you are targeting, these are simple to fix if you just read the error messages.

EDIT(2): NJones created a blog post with more details on the PdfSharp fix: Output to PDF in WPF

Answer

There is an XPS Converter that was included with PDFsharp 1.31. It's a beta version, but AFAIK it supports nearly everything and may work for your needs.

PDFsharp 1.31 can be downloaded from SourceForge or CodePlex.