iTextSharp to generate PDF from WPF FixedDocument
Asked Answered
F

4

8

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

How can generate PDF's from that, with a free and open solution, such as iTextSharp?

Fourierism answered 2/8, 2010 at 8:29 Comment(1)
I found a way to do this natively in Windows using the Microsoft PDF Printer. Check it out: https://mcmap.net/q/281931/-print-fixeddocument-xps-to-pdf-without-showing-file-save-dialogCarlottacarlovingian
G
7

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

A good open-source solution is to use the "gxps" tool that is part of GhostPDL. GhostPDL is part of the Ghostscript project and is open-source licensed under GPL2.

  1. Download GhostPDL from http://ghostscript.com/releases/ghostpdl-8.71.tar.bz2 and compile it.
  2. Copy the gxps.exe executable into your project as Content and call it from your code using Process.Start.

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
Gev answered 18/8, 2010 at 18:58 Comment(3)
Very nice (well it works, which is nice, but it would have been handy had MS included some functionality like this out of the box)...Beverle
what about licensing in this case ? I do not have to GPL2 my project because I only execute program right ? Is there any other such tool that does not have to be open source but is still free?Priapic
GPL2 requires you to make sure your program "can reasonably be considered an independent and separate work" from GhostPDL and you cannot distribute your work "as a part of a whole which is a work based on" GhostPDL. Opinions vary as to the legal implications of this. If GhostPDL is installed separately from your application and your application knows how to invoke it if present, you are almost certainly safe. You might also consider creating a unified installer to save the user from doing two separate installations.Gev
J
1

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

I have done it this way before successfully. Initially I tried to convert the control primitives (shapes) to PDF equivalents, but this proved too hard.

Johny answered 11/8, 2010 at 3:54 Comment(0)
E
0

If you can get it into an image from WPF then you can import it into iTextSharp like they do in this article. You can even avoid the filesystem all together if you write it to a MemoryStream and then use that instead of using a FileStream.

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

Enneahedron answered 18/8, 2010 at 0:50 Comment(3)
converting text and vector graphics into an image to then put it into a PDF defeats the purpose of PDF. You will not be able to select or search text or zoom in without losing quality.Heterothallic
It also creates a very large PDF, if you generate an image of sufficient DPI to allow for clear printing.Beverle
Do you have any suggestions? I agree with both these statements.Enneahedron
D
0

IF you want to do it programatically, your Best bet would be the following path XPS (Fixed Document) -> Print to PS -> Use Ghostscript to read the PS and convert to PDF. If you dont care about reading the PDF back in the code, you can print to any one of the free PDF printers to which you can pass the destination path. This way your target PDF file will still be searchable if you have any test in your report.

Delindadelineate answered 18/8, 2010 at 18:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.