ITextSharp to create PDF from WPF FixedDocument

I have a simple WPF application that displays and prints some reports with FixedDocument.

How can I generate a PDF using a free and open source solution like iTextSharp?

+7
pdf wpf itextsharp
source share
4 answers

WPF FixedDocument, also known as an XPS document, is a definite improvement over PDF. It has many features that pdf is missing. In most cases, it is better to distribute your document as XPS rather than PDF, but sometimes you need to convert it from XPS to PDF, for example, if you need to open the document on devices that have only PDF support. Unfortunately, most free tools for converting from XPS to PDF, such as CutePDF and BullzipPDF, require the installation of a printer driver or are not open source.

A good open source solution is to use the gxps tool, which is part of GhostPDL. GhostPDL is part of the Ghostscript project and is open source, licensed under the GPL2 license.

Your code might look like this:

string pdfPath = ... // Path to place PDF file string xpsPath = Path.GetTempPath(); using(XpsDocument doc = new XpsDocument(xpsPath, FileAccess.Write)) XpsDocument.CreateXpsDocumentWriter(doc).Write(... content ...); Process.Start("gxps.exe", "-sDEVICE=pdfwrite -sOutputFile=" + pdfPath + "-dNOPAUSE " + xpsPath).WaitForExit(); // Now the PDF file is found at pdfPath 
+6
source share

A simple way, which is a simple, but probably not the most efficient way, is to provide a fixed document to an image, and then embed the image in PDF using iTextSharp.

I did this before it succeeded. At first I tried to convert control primitives (shapes) into PDF equivalents, but that turned out to be too complicated.

+1
source share

If you can get it into an image from WPF, you can import it into iTextSharp, as in this article. You can even avoid the file system if you write it to a MemoryStream, and then use this instead of using FileStream.

http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images

0
source share

IF you want to do this programmatically, your best bet would be the following XPS (Fixed Document) path β†’ Print to PS β†’ Use Ghostscript to read PS and convert to PDF. If you don’t need to read the PDF back in code, you can print any of the free PDF printers to which you can pass the destination path. That way, your target PDF will still be searchable if you have any test in your report.

0
source share

All Articles