Share

Generating PDF

After generating RTF and HTML documents from a single source, and printing them using the System.Drawing.Printing.PrintDocument class, I thought it would be easy to generate PFS's. I want to provide these printable pdf's in addition to the online versions of translated sermons on the website I maintain in my weekends and evenings.

It was harder than I thought! The quickest implementation would be to let one of those free printing apps such as CutePdf generate the actual PDF's, however, if you want to programatically tell it where to save the file (instead of having a dialog pop-up) you'll have to buy a license and then it's not that elegant any way (what about the metadata for example).

Looking farther I found Docotic PDF Library from BitMiracle who kindly provided a free license for my app. I started out writing code, however, I found out it would take me a terriffic amount of time implementing a good rich text column generator.

With the printing solution I could reuse my RTF generator and use the Windows API calling SendMessageFormatRange() using an instance of a RichTextBox and passing the printers Device Context... etc, etc... getting almost all I wanted with little effort (why full justification is not implemented in the System.Windows.Forms.RichTextBox by default is a mystery to me, but here's a nice workaround).

Docotic has all you need to draw strings and text exactly where you want, but no full blown Rich Text rendering. Reuse of the windows API is not possible (their library has their own drawing methods, and does not provide any System.Drawing.Graphics object which you can pass to Windows API's).

While looking at RTF rendering, I found that WPF has exellent support for it, and the fully justified text it generates looks allot better than what the good old Windows API's produced! WPF also supports output to the XPS format, well that's almost what I want... almost...

Now to convert XPS to PDF... Searching the internet you'll find two candidates, one of them is WebSupergoo, the other is NiXPS. The problem is that they both do not have an application or OEM license, so everyone that installs your app will need to install a license of their product as well (and those licenses are not cheap!).

A great alternative is GhostXPS which can convert an XPS file to PDF by executing the program with the appropriate commandline. It is the fastest converter I know of, and it produces small files, but unfortunately (maybe it's the features I use?) it generates PDF's that dont render well in anything else but Adobe Acrobat. (I also test the output with FoxIt, PdfXchange, and Niro). I'll be whatching them though, whenever I'm aware of a new version I'll download it and test it again. An example commandline:

 

gxps-9.06-win32.exe -sDEVICE=pdfwrite -sOutputFile=test.pdf -dNOPAUSE "C:\test.xps"

 

For now I went back to the first (printing) option, but instead of CutePdef, I found PDF Forge which has an API for setting the filename etc, before printing. I use this in combination with Docotic, which fills in some extra metadata and compresses the document quite a bit. You can view the results here and here.

At my work we also had a PDF generator requirement, but then one that converted HTML to PDF. After trying quite a few we found Evo to be the best (which even handles CSS, SVG and javascript to a great extent).