简体   繁体   English

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

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

I'm trying to send a file to print without opening it trough Adobe as suggested by several Answers here; 我正尝试发送文件而不通过Adobe通过以下几个Answers建议的通过Adobe打开文件; instead, I'm using the PrintQueue library (from System.Drawing.Printing ). 相反,我使用的是PrintQueue库(来自System.Drawing.Printing )。

What I've accomplished so far: 到目前为止,我已经完成了什么:

I have the correct PrintQueue referenced as pq: 我将正确的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();

As I understood from the Microsoft Library it's correctly done but it is not working. 据我从Microsoft 了解,它已正确完成,但无法正常工作。 Any ideas? 有任何想法吗?

EDIT: Debugging the code I can see that something is being send to the printer but it seems the file is not sent. 编辑:调试代码,我可以看到某些内容正在发送到打印机,但似乎文件没有发送。

You can not just write PDF bytes to a print job. 您不能只将PDF字节写入打印作业。 The printer doesn't know how to handle it. 打印机不知道如何处理。 The RAW data you send to the printer must describe the document in a printer language specific to the printer. 您发送到打印机的RAW数据必须使用特定于打印机的打印机语言描述文档。 That's what the printer driver does. 这就是打印机驱动程序所做的。

You can not print a PDF by just sending it to the printer. 您不能仅通过将PDF发送到打印机来打印它。 You need some piece of software that renders the PDF and then sends the rendered image to the printer. 您需要一些软件来渲染PDF,然后将渲染的图像发送到打印机。

As the documentation states: 文档所述:

Use this method to write device specific information, to a spool file, that is not automatically included by the Microsoft Windows spooler. 使用此方法可以将设备特定的信息写入后台打印文件,Microsoft Windows后台打印程序不会自动包含这些信息。

I enhanced the important part of this information. 我增强了此信息的重要部分。

You need to render your PDF to the printer. 您需要将PDF呈现到打印机。 Calling the shell print verb on the file would be the ideal means to do that. 在文件上调用shell print动词将是实现此目的的理想方法。 If you insist on doing the low level rendering yourself then I recommend using a library like Ghostscript.NET and choose the mswinpr2 device as output . 如果您坚持要自己进行低级渲染,那么我建议使用Ghostscript.NET之类的库,并选择mswinpr2设备作为输出

The mswinpr2 device uses MS Windows printer drivers, and thus should work with any printer with device-independent bitmap (DIB) raster capabilities. mswinpr2设备使用MS Windows打印机驱动程序,因此应与任何具有设备无关位图(DIB)栅格功能的打印机一起使用。 The printer resolution cannot be selected directly using PostScript commands from Ghostscript: use the printer setup in the Control Panel instead. 无法使用Ghostscript中的PostScript命令直接选择打印机分辨率:而是使用“控制面板”中的打印机设置。

See SendToPrinterSample.cs for example: 有关示例 ,请参见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);
        }

If the file has to be printed in both sides you just need to add: 如果文件必须双面打印,则只需添加:

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

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

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