John Lam 1997 — 2026

Observations on Adobe Acrobat 5.0 and .NET COM Interop

I’ve been building a small set of tools that I need for my upcoming writing project. These tools will integrate some third-party tools into a standard NAnt build script. Recently, I wanted to add support for automatically generating PDF documents from my Office documents (Word and Powerpoint). I learned quite a bit of stuff while building these tools, and this entry summarizes some of these observations.

Observation 1: COM Interop. I have somehow been able to avoid COM interop until now. The first thing I learned was that VS.NET doesn’t enumerate the GAC when generating the list of .NET assemblies in the Add References dialog. Office 2003 installs its primary interop assemblies into the GAC, but fails to copy them to \program files\microsoft .net\primary interop assemblies\, which is one of the directories that are enumerated by the VS.NET add reference dialog. If you want to add additional directories to the set of directories that VS.NET enumerates for the Add References dialog, you need to add additional keys under HKLM\Software\Microsoft\VisualStudio\7.1\AssemblyFolders. I fixed my O2K3 interop problem by manually copying assemblies from the GAC to \program files\microsoft .net\primary interop assemblies\.

Observation 2: Adobe Acrobat and the entire Windows printing system are a nightmare to automate. For those of you who don’t own a copy of Acrobat, it is software that lets you generate PDF documents from any Windows program that is capable of printing. So, if I want Word 2003 to generate a PDF document, I have to make it print using a printer driver called “Acrobat Distiller”. If you’re a human being doing this, it’s no big deal. You just tell Word to Print, and when Acrobat Distiller pops up a dialog box that asks you where you want to put the PDF file, you enter it and you’re good to go.

Now imagine that you’re an app trying to automate this process. It’s a freaking nightmare. You tell Word how to print the current document via the Document.PrintOut() method. There are a million options that are available in this method, but of course, none of them are “the place where I want Adobe Acrobat Distiller to put the generated PDF”.

As it turns out, I can tell Word to print to a temporary file. This is possible because I can pass parameters to PrintOut() for the path to the generated file. In a weird quirk of implementation, this doesn’t tell Distiller where to put the PDF file. Rather it tells Distiller to generate a PS (PostScript) file!

This adds an additional step to the process: converting the PS to a PDF. To do this, you have to create an instance of Acrobat Distiller as a COM server and call its ConvertToPDF() method. You pass it the path to the PS file and the path to the output PDF file.

But wait, life isn’t set quite yet. Remember that Acrobat Distiller is a printer driver. As such, it needs to be configured. And of course, all of the default configuration settings are wrong. If you try and tell Word to print using the default configuration settings, you will get a number of errors that are expressed as popup dialogs. That would, of course, be a nightmare in an automated NAnt build script.

I still haven’t figured out how to configure printer settings programmatically yet. There really needs to be an XML configuration file for every printer driver, so you can use an XML API to whack configuration settings instead of having to learn yet another arbitrary API (YAAA) for programmatically setting printer driver settings.

In spite of all this, I’ve finally got everything working today. A suggestion to Adobe: write a simple wrapper API that solves this problem. I am more than happy to donate my code. This will eliminate one of the most common sources of frustration that are encountered by users of your product.

One final observation. If you’re running as a non-admin, you can interactively run Acrobat Distiller to your heart’s content. But if you want to start up Acrobat as a COM server, it doesn’t work. This smells like a DCOM permissions problem, but my first look around here doesn’t fix the problem. Grrr.

5 archived reader comments

Andy Hopper (2003-09-20)

Might I recommend Win2PDF? http://www.win2pdf.com It performs the same duties Distiller does and fully supports the ability to pass a destination filename to Word’s PrintOut method. Only costs $35, too. I used it for a project that automatically converts hundreds of documents from Word to PDF every day, and it worked very well.

John Lam (2003-09-20)

Thanks for the link, Andy. What’s interesting about their product is that they chose to emulate the old Adobe way of specifying the filename for the output PDF file - via a registry key. While a nasty hack, it’s necessary until we get some facility for passing XML manifest files to embedded drivers in applications.

RichB (2003-09-20)

I fixed my O2K3 interop problem by manually copying assemblies from the GAC to \program files\microsoft .net\primary interop assemblies.

What’s your take on the versioning issues with regards to a common location for PIAs?

Andy Hopper (2003-09-21)

Hmm. Using the registry is just one way of accomplishing that - my apps simply used the OutputFilename parameter of PrintOut and never mucked about with the registry.

Pierre (2003-09-23)

I have quit using NAnt and instead am using Menees’ MegaBuild tool, which is great for automating VS.NET builds without starting VS.NET.

The utility can do lots of stuff and has a nice GUI. Check it out at :

http://mywebpages.comcast.net/bmenees/index.html

Bye.