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...