WPF to XPS to PDF

10,588

Solution 1

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.

Solution 2

If a commercial SDK would be an option, there is one company I came across that has a professional XPS to PDF conversion option (and more).

http://www.nixps.com

Warning: I'm not affiliated with this company nor have I tried their products. I have on the other hand heard good things about them.

Solution 3

IMHO, you've got the right idea: XPS is, in my mind, the simplest method of serializing the data into a usable format, especially for conversion.

As for a third party tool, I've a peer (as in, not myself,)who's used CheckPrixa with some measure of success; it's freeware and it offers command line conversions. (i.e. serializing a document and converting a la generated .bat file.)

It also allows you to handle keywords, titles, etc. along with more important items like even pagination.

As far as I'm aware, there are no known discrepancies.

Alternatively, if converting the MSXPS is what's giving you headaches, you might want to look at XpsConverter, it converts MSXPS to OpenXPS, and as a Window's driver, it's available on any machine using .NET 3 and above.

Keep us updated with whatever solution you try, virtual reports are a growing deal.

Solution 4

I use Telerik Reporting which handles document pagination and allows you to create reports in a designer similar to creating a GUI. I decided to just buy a 3rd party product because I didn't want to spend development time writing my own paginator, etc. Also, this tool is probably more bug proof then a custom solution and allows a lot of flexibility for how you want to design your printout/report. In the future if you have more complicated printing or reports required, this tool does a lot more. I do not work for Telerik, but there product is good.

It allows exporting to PDF, Word, Excel, HTML, PowerPoint, PNG, etc. etc.

Share:
10,588
Alex Hope O'Connor
Author by

Alex Hope O'Connor

Software Engineer working for DSITIA in Brisbane Australia.

Updated on June 17, 2022

Comments

  • Alex Hope O'Connor
    Alex Hope O'Connor about 2 years

    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

    • Lubo
      Lubo over 11 years
      I am using custom DocumentPaginator for printing too. PDF and XPS are produced by printing to a virtual printer. It works well also for large documents.
    • Alex Hope O'Connor
      Alex Hope O'Connor over 11 years
      I am trying to avoid using a virtual printer as I want to minimise user dependencies.
    • N Jones
      N Jones over 11 years
      @AlexHopeO'Connor I'm in the same boat you were with this question and would much appreciate it if you shared the fix you made to PDFSharp 1.31.
    • Alex Hope O'Connor
      Alex Hope O'Connor over 11 years
      @NJones added more details to my question
    • N Jones
      N Jones over 11 years
      @AlexHopeO'Connor Thanks! I got things working, but had to make a number of other bugfixes. I blogged about it here: nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free
    • Alex Hope O'Connor
      Alex Hope O'Connor over 11 years
      @NJones the link to the project diff seems to be broken.
    • N Jones
      N Jones over 11 years
      Sorry--mime type set wrong. Should work fine now.
    • Shahin Dohan
      Shahin Dohan over 4 years
      There's a native Windows way to convert XPS or FixedDocument to PDF using the Microsoft PDF Printer: stackoverflow.com/q/58517394/1469494
  • Alex Hope O'Connor
    Alex Hope O'Connor over 11 years
    This worked perfectly, however I was wondering if maybe you could elaborate on why the XPS converter is not present in version 1.32? Has it been discontinued?
  • Alex Hope O'Connor
    Alex Hope O'Connor over 11 years
    I would rather make do with open source tools
  • Alex Hope O'Connor
    Alex Hope O'Connor over 11 years
    PDFSharp with some slight modification worked out perfectly, however I did have to go back a version and compile the source with custom fixes which I found via google.
  • I liked the old Stack Overflow
    I liked the old Stack Overflow over 11 years
    I asked Stefan (developer of PDFsharp). The XPS converter is not discontinued, but somewhat hibernated. He'd like to re-arrange (rewrite) some parts, but doesn't know when he'll find the time (I think some of the missing details will lead to bigger changes). AFAIK the XPS converter should also work with PDFsharp 1.32 (but you have to download 1.31 to get the converter source). Glad to hear it works for you. Never tried it myself, but the demos I saw are impressive.
  • N Jones
    N Jones over 11 years
    I for one am looking forward to when he'll pick it back up again. In the meantime, I dove into it and found a few additional bugs that will clear up a few problems. I blogged about it here: nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free
  • Kind Contributor
    Kind Contributor over 8 years
    Apparently there is a 50 page limit in the official source, you'll need to modify and compile yourself (if there isn't already such a fork)
  • Meer
    Meer about 8 years
    @user-007 i have converted xps to pdf using PDF sharp but id does't show me on PDF while it shows image in xps.
  • Louis Somers
    Louis Somers almost 5 years
    Are they still in business? I tried to send an email to sales and it bounced.
  • David van Driessche
    David van Driessche almost 5 years
    Last I heard they don't exist directly anymore but have been acquired by Hybrid Software: hybridsoftware.com
  • Ashish Gehlot
    Ashish Gehlot about 2 years
    XpsConverter is provided by Microsoft which seems can't be used due to copyright issues mentioned on their website. "The XpsConverter is not intended to be used in any other capacity than as a stand-alone tool. It is not supported for any other use. It may not be used in part or whole in any application or driver, and de-compiling or modifying the tool is strictly prohibited. Microsoft retains all rights and holds copyright on XpsConverter.exe and all its supporting documentation."