0

Well, my objective is to print a PDF file without asking for user confirmation.
I can't force my users to use a specific PDF reader (such as Adobe Acrobat or Foxit) and need to print the file without any user interaction.

My current code is the following:

String strFile = "pdf_file.pdf";
String strPrinter = "Printer Name";
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = strFile;
p.StartInfo.Arguments = strPrinter;
p.StartInfo.Verb = "PrintTo";
p.StartInfo.CreateNoWindow = true;
p.Start();
p.CloseMainWindow();

I have some code to kill the process if it don't exit, but it doesn't matter now.

This code works mostly well, but i can't get the error messages that occur in the process.
The try catch block won't help in this case, because the error occur in the process "p", not in the main process.

Searching the net i found out that to recover the errors i need to set the following:

p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
// Start process, kill it, etc...
String s = p.StandardError.ReadToEnd();

But applying this code gives me another error: the file is not a valid win32 application.
Obviously, the file I'm oppening is a PDF file, not a EXE.

Anyone knows another way to recover the errors that occur in the process?
Such as the printer is not found, etc...

Dinei
  • 4,494
  • 4
  • 36
  • 60
  • For starters, you're trying to start a PDF file as if it's an executable. PDFs are not executable. – ElGavilan Apr 17 '14 at 12:38
  • It was exactly what I said in "Obviously, the file I'm oppening is a PDF file, not a EXE". It works well when `p.StartInfo.UseShellExecute = true;` – Dinei Apr 17 '14 at 12:45
  • Take a look at this: http://stackoverflow.com/questions/21462247/printing-pdf-using-ghostscript-net-dpi-printing-issue – HABJAN Apr 18 '14 at 07:35

1 Answers1

1

If I understood it right, you are trying to print a PDF file using the default PDF reader installed in the client machine.

As you said, this code is trying to actually run a .pdf file. You could use a C# PDF library, but I dont know how it'll send the raw PDF data to the printer, so I would try using a PDF command line tool, put it in the bin/Release folder of the project (and also bin/Debug, for testing purposes) and then call it using a command line.

If you want it to be totally transparent and PDF-Reader independent, maybe you should try it. In the command line, you explicitly tell what executable you want to run, so it wont make Adobe Acrobat Reader pop up in the screen, and the user won't freak out =D

Here is an example of PDF printer: http://pdfbox.apache.org/commandline/#printPDF

The command line is: java -jar pdfbox-app-x.y.z.jar PrintPDF [OPTIONS] <inputfile>

It needs Java to run, if this is not a problem, you can try it. You could also search for a native or .NET managed solution, I think you got the idea.

Good luck!

Ricardo Pieper
  • 2,613
  • 3
  • 26
  • 40
  • 1
    Well, I cannot force the users to have de Java installed in their machines, so I made a search and found [this question](http://stackoverflow.com/questions/2599925/how-to-print-pdf-on-default-network-printer-using-ghostscript-gswin32c-exe-she/2600189#2600189), where is used the [Ghostscript](http://ghostscript.com/) software to do the silent print. I will try this software, but I believe that it'll fulfill my requirements. Thank you. – Dinei Apr 17 '14 at 13:34