C# - LPR Command to Print PDF Files

我与影子孤独终老i 提交于 2019-12-24 11:05:51

问题


I am trying to run an LPR command to print a PDF. The code I'm using is being executed from a button click in a windows forms application.

Code:

var command = @"lpr –S 192.168.1.245 –P DAILY C:\Test.pdf";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardError = true;
procStartInfo.CreateNoWindow = true;

// start process
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();

proc.WaitForExit();

// read process output
string cmdError = proc.StandardError.ReadToEnd();
string cmdOutput = proc.StandardOutput.ReadToEnd();

The program is running as an x64 program, so it can find lpr program in the C:\Windows\System32 folder.

When the code executes the error string is empty and the output string contains the following (same output as if the command run had been lpr /?)

Output:

Sends a print job to a network printer

Usage: lpr -S server -P printer [-C class] [-J job] [-o option] [-x] [-d] filename

Options: -S server Name or ipaddress of the host providing lpd service -P printer Name of the print queue -C class Job classification for use on the burst page -J job Job name to print on the burst page -o option Indicates type of the file (by default assumes a text file) -x Compatibility with SunOS 4.1.x and prior -d Send data file first

If I copy and paste the command exactly as it appears in the code and paste it into a command window, even if it's the SAME command window the application opened, it works fine.

Does anyone have any insight into why this would be happening? Thanks in advance!


回答1:


I was able to figure it out. If it helps anyone else, the code that is working is

var command = @"lpr -S 192.168.1.245 -P ""DAILY"" ""C:\Test.pdf""";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/C " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardError = true;
procStartInfo.CreateNoWindow = true;

// start process
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();

proc.WaitForExit();

// read process output
string cmdError = proc.StandardError.ReadToEnd();
string cmdOutput = proc.StandardOutput.ReadToEnd();

Notice that the Queue Name and Filename are surrounded by quotes. My other issue was that I'd originally copied the command from an email so the dash was wrong, I had to delete and manually type the dashes in the command for it to be recognized. If you look really close in the original question, you can see the dash is slightly longer.

For information on making sure the lpr command is available, especially if you're running a 32 bit app on a 64 bit machine, see the following references.

Reference 1: lpr command not working from my C# program in Win 7

Reference 2: http://www.tomshardware.com/forum/240019-44-error-windows



来源:https://stackoverflow.com/questions/46352551/c-sharp-lpr-command-to-print-pdf-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!