繁体   English   中英

使用System.Drawing.Printing将文件发送到打印无法正常工作

[英]Sending file to print not working using System.Drawing.Printing

我正尝试发送文件而不通过Adobe通过以下几个Answers建议的通过Adobe打开文件; 相反,我使用的是PrintQueue库(来自System.Drawing.Printing )。

到目前为止,我已经完成了什么:

我将正确的PrintQueue引用为pq:

PrintQueue pq; //Assume it's correct. The way to get here it isn't easy and it is not needed for the question.

// Call AddJob
PrintSystemJobInfo myPrintJob = pq.AddJob();

// Write a Byte buffer to the JobStream and close the stream
Stream myStream = myPrintJob.JobStream;
Byte[] myByteBuffer = ObjectIHave.ToArray(); //Ignore the ObjectIhave, it actually is Linq object which is correct as well.
myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
myStream.Close();

据我从Microsoft 了解,它已正确完成,但无法正常工作。 有任何想法吗?

编辑:调试代码,我可以看到某些内容正在发送到打印机,但似乎文件没有发送。

您不能只将PDF字节写入打印作业。 打印机不知道如何处理。 您发送到打印机的RAW数据必须使用特定于打印机的打印机语言描述文档。 这就是打印机驱动程序所做的。

您不能仅通过将PDF发送到打印机来打印它。 您需要一些软件来渲染PDF,然后将渲染的图像发送到打印机。

文档所述:

使用此方法可以将设备特定的信息写入后台打印文件,Microsoft Windows后台打印程序不会自动包含这些信息。

我增强了此信息的重要部分。

您需要将PDF呈现到打印机。 在文件上调用shell print动词将是实现此目的的理想方法。 如果您坚持要自己进行低级渲染,那么我建议使用Ghostscript.NET之类的库,并选择mswinpr2设备作为输出

mswinpr2设备使用MS Windows打印机驱动程序,因此应与任何具有设备无关位图(DIB)栅格功能的打印机一起使用。 无法使用Ghostscript中的PostScript命令直接选择打印机分辨率:而是使用“控制面板”中的打印机设置。

有关示例 ,请参见SendToPrinterSample.cs

        string printerName = "YourPrinterName";
        string inputFile = @"E:\__test_data\test.pdf";

        using (GhostscriptProcessor processor = new GhostscriptProcessor())
        {
            List<string> switches = new List<string>();
            switches.Add("-empty");
            switches.Add("-dPrinted");
            switches.Add("-dBATCH");
            switches.Add("-dNOPAUSE");
            switches.Add("-dNOSAFER");
            switches.Add("-dNumCopies=1");
            switches.Add("-sDEVICE=mswinpr2");
            switches.Add("-sOutputFile=%printer%" + printerName);
            switches.Add("-f");
            switches.Add(inputFile);

            processor.StartProcessing(switches.ToArray(), null);
        }

如果文件必须双面打印,则只需添加:

switches.Add("-dDuplex");
switches.Add("-dTumble=0");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM