3

On my machine, when selecting multiple PDF documents in Windows Explorer, right clicking and choosing Print, Adobe Acrobat Reader is opened minimized and all documents are silently sent to the printer.

I want to do the same as Windows does, but how is it done?

I am using ProcessStartInfo with the PrintTo verb, as you can see in a previous question. Unfortunately, this is everything but silent, and I am having big problems when printing more than one document. How do I know when the application (typically Adobe Reader) is done printing? If starting several printTo processes in a row, one document seems to open before the previous is finished printing.

I would like to avoid hard coding Adobe Reader, because some of my users have exchanged it for Foxit Reader or others.

Community
  • 1
  • 1
Skywise
  • 429
  • 4
  • 12
  • We had the same (and a related) problem and finally used [gsprint](http://pages.cs.wisc.edu/~ghost/gsview/gsprint.htm) as a workaround. – Silas Dec 21 '11 at 14:06
  • First thing I'd do is check exactly what is happening on your machine, by digging around with `regedit` (carefully of course). – AakashM Dec 21 '11 at 14:10
  • Ok @Silas, I was hoping not to come to that conclusion. =/ – Skywise Dec 21 '11 at 14:23
  • @AakashM - can you elaborate on that? – Skywise Dec 21 '11 at 14:23
  • In `regedit`, first see what's the value in the `(Default)` key at `HKCR\.pdf`. Then look at `HKCR\`, drilling down to `shell\print\command`. There, you should find the exact command line that Acrobat is invoked with when you do the operation you describe in Windows Explorer. Does invoking that exact command give you the right results? (if this is all incomprehensible, just say and I will post an answer with pictures) – AakashM Dec 21 '11 at 14:27
  • 2
    Thanks @AakashM, looks like it is doing `"C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe" /p /h "%1"`, which should correspond to a minimized window, straight to print, with that document. I guess I could hard code that behaviour, but what about the users not having Acrobat reader? I guess I could have one solution if Acrobat was found, and another if it was not found... – Skywise Dec 21 '11 at 14:34
  • If you want to be really helpful, you could have your app go and do that same lookup itself, I suppose? – AakashM Dec 21 '11 at 14:36
  • Shoot, I was typing my response while you were all answering basically the same thing in the comments. No foul intended. – djdanlib Dec 21 '11 at 14:40
  • Yes @AakashM, I should do that in the app. I can easily check if the acrobat .exe file is there, and choose another print method if not. But is there a way of asking the OS for the path of the .exe for handling pdf files? – Skywise Dec 21 '11 at 15:14
  • I mean, your app can look in the registry for the registered handler for PDF files, and invoke it. But there is probably a better way, not sure personally. – AakashM Dec 21 '11 at 15:18
  • @Silas, do you know if GhostscriptSharp can be used to print PDF docs? – Skywise Dec 22 '11 at 11:49
  • @Skywise: I didn't know GhostscriptSharp, sorry. – Silas Dec 22 '11 at 14:11

1 Answers1

5

I don't know if you can do it with other PDF viewers in such a way that those viewers are plug-and-play replacements for Adobe Reader. You'll probably have to tailor it to each program you want to support. It's not so difficult to have Reader on one's system, really, if it's necessary to perform a job and most computers come with it preinstalled.

The first thing you have to know is that when you tell it to print via that verb, via either code or the Explorer context menu, you are doing something like this:

""C:\Program Files\Adobe\Reader 10.0\Reader\AcroRd32.exe"" /p /h "%1" 

Note the arguments: /p (tells it to print) and /h (start minimized).

There's another option. The Adobe Developer FAQ ( http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/intro_to_sdk/DeveloperFAQ.pdf ) states that this command line works per-file:

AcroRd32.exe /t path "printername" "drivername" "portname"

The document specifies that this initiates Adobe Reader and prints a file, whose path must be fully specified, while suppressing the Print dialog box. (Copy-pasted from Developer FAQ.)

There is also an option /n which "launches a separate instance of Acrobat or Adobe Reader, even if one is currently open." (Developer FAQ again.) That could be used to run multiple print jobs in parallel, I imagine.

I found yet another command line reference at: Adobe Reader Command Line Reference

So basically, you could iterate your list of PDFs, and for each one start a new print process with a Process.Start call and wait for it to close via a Process.WaitForExit. This will make your program appear to hang and I hate when programs hang while they perform operations, so you should really do this in a cancellable BackgroundWorker that reports progress and still leaves your GUI somewhat interactive.

Community
  • 1
  • 1
djdanlib
  • 21,449
  • 1
  • 20
  • 29
  • Thanks, @djdanlib. I have confirmed now that I can assume that Acrobat Reader is present, and so I can use your `Acrobat Reader /t` solution. It does print, but adding `/h` does not make it minimized. – Skywise Dec 21 '11 at 15:00