If you don’t have to, please don’t reinvent the wheel. I am a proponent of just going out and buying the tools you need to do your job, rather than re-making them yourself. It’s a bit inefficient to solve the same problem that others have already solved. With that said, the best tool on the market for running PDF prints is RTV Tools. Get that if you can spend a few bucks. The rest of us poor souls, read on!
I put Revit’s name in the title, but to be honest the responsibility for this lies somewhere in-between Autodesk and Adobe. First, let’s make it clear, that for a piece of software that main’s job is to create documentation drawings of buildings (99% of the time distributed in PDF format), not to have its own PDF creator, or at least licensed and implemented one of the handful available ones…I just have no words for this Autodesk. This condition, that we have been dealing with since Revit’s inception some 15+ years ago, is just not acceptable. Sure, it’s hunky-dory to let users specify their own Printer, but for god’s sake, Autodesk, please also give them an embedded one that comes with the software. That would also allow you to cater to needs of those of us, that want a little more out of the software than a typical end user. I am speaking of the development community that wants to build automation tools on top of your software, and having access to embedded PDF creator, with ability to specify things like JPG Compression, Security protocols or simply FILE NAME. Yes! File name, and location that will not be overridden by the 3rd party PDF creator like Adobe’s Distiller. Now that we are on the subject of Adobe and its free PDF Reader, I just have no words for how shitty that thing is. Yes, it’s free, but it’s also severely crippled, and ON PURPOSE! What the hell were you thinking Adobe, always launching Acro Tray process in the background, and leaving it open even, when printing is done. WTF? Yes, the paid version of your software has more support, but still…that’s just sad. Anyways, let’s talk about some solutions here:
You probably know that if you set Printing Preferences for Adobe PDF printer, like this…
…you should be able to send a printing job of multiple sheets, and you would not be prompted to specify location for each one individually. That’s great if you are content with the pre-defined print location. What happens when you are building a plug-in and you want your users to be able to specify custom file names and file locations? In that case there is so far been one option:
Override the PrinterJobControl registry input! What?! Yes, in order to specify a file name, one must dabble with Registry. Thanks Autodesk! That’s swell! Here’s how and why:
Adobe PDF, normally prompts user for a new file location. However, there is a way to override that, if one overrides the PrinterJobControl subkey, with a Key/Value pair specifically telling Adobe that for the next printing job submitted from Revit.exe process, you want it to print to a specific filename and location. Here’s a small utility that will do that for you:
As you can see this will only work for Adobe PDF printer. I also have a global variable named CURRENT_REVIT_VERSION stored in my Settings file. This should be either 2016 or 2017 depending on what version of Revit I am compiling my plug-in for. Basically, you would need to call this method before every single sheet is submitted to Revit’s Print Manager for printing. That way, you always override the next print job’s name, and location and Adobe won’t prompt for filename.
Now, it might ask to replace an existing file if you didn’t uncheck that setting in the Printing Preferences dialog box. To avoid seeing that prompt, just use the regular File.Exists() method and File.Delete() to clean up before printing. Something like this will do:
Now, I also often do a post processing routine. Take for example, Zipping up all of your files to be sent to consultant or even combining them into a single PDF, if you are that adventurous. Since printing a PDF can take quite some time, even minutes, which in computer world is freaking decades, you will need a small utility to wait for the last file to print before starting your post processing. Here’s one I copy-pasted from Stack Overflow (copying and pasting from SO is an art.):
I call it like this:
The “lastCreatedFileName” variable is as you can guess the name of the last sheet that I have submitted to Print Manager. I am only really checking the last file submitted, since all others would be done, before I can submit my last one. My wait routine, has 1000 loops each halting the execution for 300 ms for a total of 3min. If printing of your last sheet takes more than 3min, it will most likely be skipped in the post-processing routine. Just an FYI.
I hope this helps. Cheers!