How to use the PDFTron PDFNet virtual printer directly?

Question:

How do I interact directly with the PDFTron PDFNet virtual printer?

Answer:

While the PDFTron PDFNet virtual printer is normally hidden behind the ToPDF interface, it possible to use it directly for your own specific usage. This is done through the following registry.

HKEY_CURRENT_USER\\SOFTWARE\\PDFTron\\PDFTron PDFNet

The following sub keys are used under PDFTron PDFNet.

printto : which tells the virtual printer where to save the XPS data to.

jobid : the current job being worked on

jobidComplete : last jobid completed

The jobid and jobidComplete keys are filled in by the virtual printer, and therefore are read-only for you. The printto variable is the key you modify.

Note, that this interface implies serialization, and PDFTron PDFNet virtual printer does not support concurrency, so you need to synchronize your requests.

With the following information, this is an example of using the PDFTron PDFNet virtual printer to convert any printable file format to PDF.

  1. Install our Virtual Printer, by calling Convert.Printer.Install. This requires Admin/Elevated rights, and can be done just once as part of the installation to your server the first time. Once installed, you no longer need to call this code.

  2. When time comes to Print the document, your application would set the following registry key with a temp destination location, such as %TEMP%\.xps

HKEY_CURRENT_USER\SOFTWARE\PDFTron\PDFTron PDFNet\printto

  1. Then you would pass the PDFTron PDFNet printer name to the 3rd party software to print normally. The PDFTron PDFNet virtual printer will check the registry and print to that location.

  2. At this point the virtual printer will fill in jobid, and then later jobidComplete. This info helps with tracking progress, and indicates if printing failed.

  3. Once printing is indicated to be over, you would also check that there is a file at the temp path location. Unfortunately the Windows printing interface is not that reliable. For example there is no way to know if the print job was cancelled by the user, or it failed somehow another way. So jobIdComplete, OS returns codes, and of course temp file generation, should together be used to decide if the print was successful or not.

  4. Once you are satisfied that you got the XPS, time to convert to PDF.

PDFDoc doc = new PDFDoc(); Convert.FromXPS(doc, "%TEMP%\\<GUID>.xps"); doc.Save("path_to_save.pdf");

  1. Finally clean up the temp xps file

Our convert test sample code shows how to handle installing/uninstalling, and also changing the printer name (very simple branding if you like). Note the sample always uninstalls, but as mentioned above, install once as admin, and then just leave it there. The only code you would need to run every time the process launches would be to set the printer name if you were not using the default “PDFTron PDFNet” name.