如何使用GhostScript(gswin32c.exe)shell命令在默认网络打印机上打印PDF

我想通过GhostScript在Windows的网络打印机上打印PDF文件。
(我不想使用Adobe Reader)

我已经阅读了gswin32c.exe ,它可以完成这项工作。
我尝试了许多命令,并且没有找到如何强制gs在我的(Windows默认)网络驱动器上打印PDF的方法。

我不需要点精确网络打印机 – 默认可以使用。 但如果没有这样的选择,我也很高兴通过打印机名称。 (我尝试过使用param -SDevice =“\ server_IP \ printer_name”,但这也不起作用……)

在Windows cmd下运行的命令:

gswin32c -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=1 -sDEVICE=ljet4 -sOutputFile="\\spool\\\Server_Name\Printer_name" "C:\test.pdf" 

基于上面创建的方法 – 没有工作和thorwsexception。 (错误代码= 1)

  ///  /// Prints the PDF. ///  /// The ghost script path. Eg "C:\Program Files\gs\gs8.71\bin\gswin32c.exe" /// The number of copies. /// Name of the printer. Eg \\server_name\printer_name /// Name of the PDF file. ///  public bool PrintPDF (string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Arguments = " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\" + printerName + "\" \"" + pdfFileName + "\""; startInfo.FileName = ghostScriptPath; startInfo.UseShellExecute = false; Process process = Process.Start(startInfo); return process.ExitCode == 0; } 

知道如何让它在C#下工作吗?

我终于使其工作且易于调试。
我感兴趣的最终方法代码:

  ///  /// Prints the PDF. ///  /// The ghost script path. Eg "C:\Program Files\gs\gs8.71\bin\gswin32c.exe" /// The number of copies. /// Name of the printer. Eg \\server_name\printer_name /// Name of the PDF file. ///  public bool PrintPDF (string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Arguments = " -dPrinted -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=ljet4 -sOutputFile=\"\\\\spool\\" + printerName + "\" \"" + pdfFileName + "\" "; startInfo.FileName = ghostScriptPath; startInfo.UseShellExecute = false; startInfo.RedirectStandardError = true; startInfo.RedirectStandardOutput = true; Process process = Process.Start(startInfo); Console.WriteLine( process.StandardError.ReadToEnd() + process.StandardOutput.ReadToEnd() ); process.WaitForExit(30000); if (process.HasExited == false) process.Kill(); return process.ExitCode == 0; } 

您应该首先从命令行测试您的选项,然后将成功转换为您的代码。

PDF文件通常已包含页边距。 您“经常剪切”页面内容可能来自PDF格式,该格式适用于以Letter格式打印的A4页面尺寸。

PDF还使用一些内部框来组织页面(和对象)内容: MediaBoxTrimBoxCropBoxBleedbox

有多种选项可以控制“媒体大小”Ghostscript呈现给定输入:

 -dPDFFitPage -dUseTrimBox -dUseCropBox 

使用PDFFitPage Ghostscript将渲染到当前页面设备大小(通常是默认页面大小)。

使用UseTrimBox ,它将使用TrimBox (它将同时将TrimBox设置为该值)。

使用UseCropBox ,它将使用CropBox (它将同时将UseCropBox设置为该值)。

默认情况下(不提供参数),Ghostscript将使用MediaBox渲染。

注意,您还可以使用-sPAPERSIZE (在Ghostscript知道的所有预定义值中选择)或(为了更大的灵活性)使用-dDEVICEWIDTHPOINTS=NNN -dDEVICEHEIGHTPOINTS=NNN来设置自定义页面大小,从而另外控制输出的整体大小。

不确定它是否对任何人有帮助,但是将打印文档添加到队列而不是立即打印对上一部分进行更改

 startInfo.Arguments = " -dPrinted -dNoCancel=true -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies=" + Convert.ToString(numberOfCopies) + " -sDEVICE=mswinpr2 -sOutputFile=%printer%" + printerName + " \"" + pdfFullFileName + "\""; 

先决条件:将打印机的作业类型配置为“保留打印”:在我们的例子中,我们有一台Rico Aficio MP 4000打印机 ,我们的用途是运行夜间作业来打印通过SSRS生成的一堆PDF文件。