C#-LPR命令以打印PDF文件 [英] C# - LPR Command to Print PDF Files

查看:604
本文介绍了C#-LPR命令以打印PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行LPR命令以打印PDF.我正在使用的代码是通过Windows窗体应用程序中的按钮单击执行的.

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.

代码:

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();

该程序作为x64程序运行,因此可以在C:\ Windows \ System32文件夹中找到lpr程序.

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

执行代码时,错误字符串为空,并且输出字符串包含以下内容(与命令运行之前为lpr /?的输出相同)

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 /?)

输出:

将打印作业发送到网络打印机

Sends a print job to a network printer

用法:lpr -S服务器-P打印机[-C类] [-J作业] [-o选项] [-x] [-d]文件名

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

选项: -S服务器提供lpd服务的主机的名称或ipaddress -P打印机打印队列的名称 -C类作业分类,用于突发页面 -J作业要在连发页面上打印的作业名称 -o选项指示文件的类型(默认情况下为文本文件) -x与SunOS 4.1.x及更低版本的兼容性 -d首先发送数据文件

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

如果我完全按照代码中显示的命令复制并粘贴命令并将其粘贴到命令窗口中,即使它是应用程序打开的SAME命令窗口,也可以正常工作.

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!

推荐答案

我能够弄清楚.如果对其他人有帮助,那么正在运行的代码就是

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.

有关确保lpr命令可用的信息,尤其是在64位计算机上运行32位应用程序的情况下,请参见以下参考资料.

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.

参考文献1:参考文献2: http://www.tomshardware.com/forum/240019-44-error-windows

这篇关于C#-LPR命令以打印PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆