本文介绍了从C#-Program进行PDF打印,并在特定纸盘上使用Ghostscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的软件中,我正在创建pdf文件,并使用ghostscript将它们打印出来,如下所示:

...
string[] printParams = new string[] {
"-q",
"-sDEVICE=mswinpr2",
"-sPAPERSIZE=a4",
"-dNOPAUSE",
"-dNoCancel",
"-dBATCH",
"-dDuplex",
string.Format(@"-sOutputFile=""\\spool\{0}""", printerName),
string.Format(@"""{0}""", filename)
...
var p = new Process();
p.StartInfo.FileName = this.ghostScriptExePath;
p.StartInfo.Arguments = string.Join(" ", printParams);
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
...

到目前为止,它工作正常,并且可以在指定的打印机上打印出来.

我的问题是,在某些情况下,我想从特定的纸盘中打印出pdf文档.

这可以用ghostscript来实现吗?

我知道,我可以在已安装的设备上添加打印机两次,一次是在纸盘1上,一次是在纸盘2,但是要在所有受影响的客户端PC上进行配置会花费很多精力.

谢谢您的帮助!

卡尔

解决方案

仅是一个我确定可以使用的想法:

由于您使用的是 Ghostscript ,因此可以将PDF光栅化为图像,然后打印图像通过使用 PrintDocument 类已经在.NET框架中构建.这样,您可以通过设置 PageSettings.PaperSource .请看以下示例: 如何使用C#为PrintDocument选择不同的纸盘

要从C#代码中更简单地使用Ghostscript,可以使用 Ghostscript.NET ,这是Ghostscript库的托管包装.查看有关如何将PDF光栅化为图像的示例: GhostscriptRasterizer示例 .

Ghostscript.NET 也可以通过NuGet获得: http://www.nuget.org/packages/Ghostscript.NET/

如果您想使用Ghostscript进行所有操作,则可以将PDF转换为Postscript,解析该Postscript文件,通过添加进纸匣选择代码对其进行修改,然后打印Postscript文件.

in my current software I'm creating pdf-files and printing them out with ghostscript like this:

...
string[] printParams = new string[] {
"-q",
"-sDEVICE=mswinpr2",
"-sPAPERSIZE=a4",
"-dNOPAUSE",
"-dNoCancel",
"-dBATCH",
"-dDuplex",
string.Format(@"-sOutputFile=""\\spool\{0}""", printerName),
string.Format(@"""{0}""", filename)
...
var p = new Process();
p.StartInfo.FileName = this.ghostScriptExePath;
p.StartInfo.Arguments = string.Join(" ", printParams);
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
...

So far it works just fine and prints out on the specified printer.

My problem is, that I want to print out my pdf-document from a specific paper tray in some cases.

Can this be achieved with ghostscript?

I know, that I can add a printer twice to my installed devices, once with tray 1 and once with tray 2, but this would be a lot of effort to configure on all effected client-PCs.

Thank you for your help!

Karl

解决方案

Just an idea for which I'm sure it will work:

Since you are using Ghostscript, you can rasterize your PDF's to images and then print images by using PrintDocument class already built in the .NET framework. This way you can choose which tray to use by setting PageSettings.PaperSource to a different tray. Take a look at this example: How to select different tray for PrintDocument with C#

For simpler Ghostscript usage from you C# code, you can use Ghostscript.NET, a managed wrapper for the Ghostscript library. Take a look at this sample on how to rasterize PDF to image: GhostscriptRasterizer Sample.

Ghostscript.NET is also available via NuGet: http://www.nuget.org/packages/Ghostscript.NET/

If you want to do everything by using Ghostscript, you could convert your PDF's to Postscript, parse that Postscript files, modify them by adding tray select code and then print Postscript files.

这篇关于从C#-Program进行PDF打印,并在特定纸盘上使用Ghostscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:43