Batch printing pdf files

The Problem: Printing a Pdf

Lately I needed to print pdfs (not create them by using a pdf printer, which is a different matter and there are free products available like the adfree Bullzip Pdf Printer) programmatically. After searching the web I found some notes about using acrobat reader to do the printing, some using commandline switches which only work for old versions of the acrobat reader, some using DDE like (here and here), which bring dependencies of it's own to the game and if you read on adobes support page "These are unsupported but have worked for some developers." well than thats not what I was looking for. Both solutions are known to leave a lingering instance of the acrobat reader in the background, as the newer versions don't seem to exit after doing their job anymore.

Of course there are commercial solutions available, so I tried the demo version, but it wasn't even able to display the 1-Page sample document I fed it, which was rendered by the reporting services, as the only thing that showed up was the trial watermark. With some other pdfs I had laying around I was more lucky, at least some of the pages were legible.

The Solution: Foxit reader + cmdline switch '/t'

I am using the foxit pdf reader for reading pdfs on my machine, which is a rather lightweight viewer (the download is 5.3 MB, it consists of only 1 executable [9.2 MB] and no dlls) compared to the acrobat reader (even the Download is about 27 MB). Sure enough it supports printing the pdf and it also supports silent printing (with no ui) to the default printer by using explorer's context menu on a pdf file of your choice. So I took a look in the registry to find the commandline switch (which btw. is '/p', who would have guessed :-p) and I found a second switch '/t' named 'PrintTo' under 'HKEY_LOCAL_MACHINE\SOFTWARE\Classes\FoxitReader.Document\shell\printto\command' which sounded even more interesting. PrintTo uses the '/t' switch and supports more than one argument, which you can use to specify the printers name. In comparison to the acrobat reader, Foxit's reader is a good citizen and exits after doing it's job so you can just invoke it and keep it's process WaitHandle to be notified when it has done it's work.

You can use it like: foxit reader.exe /t sample.pdf Printer1 where Printer1 is the name of your printer

Advantages

  • Easy
  • Silent
  • Free
  • Can choose the printer
  • you can script it