Reputation: 5464
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...
Upvotes: 0
Views: 1296
Reputation: 2702
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!
Upvotes: 1