2017-02-15 116 views
0

我正在研究一个应用程序,我必须在原始打印文本之后放置一行额外的文本。PrintQueue.AddJob方法不起作用

为此,我正在使用FindFirstPrinterChangeNotification和FindNextPrinterChangeNotification打印后台处理程序API的方法工作正常。

我能够得到打印队列,显示1

我使用下面的代码作业计数的数量在打印队列中添加一个新的任务:

 // Create the printer server and print queue objects 
     LocalPrintServer localPrintServer = new LocalPrintServer(); 
     PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue(); 

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

     // Write a Byte buffer to the JobStream and close the stream 
     Stream myStream = myPrintJob.JobStream; 
     Byte[] myByteBuffer = UnicodeEncoding.Unicode.GetBytes("This is a test string for the print job stream."); 
     myStream.Write(myByteBuffer, 0, myByteBuffer.Length); 
     myStream.Close(); 

我的代码成功执行,没有任何例外,但新作业不会与原始文本一起打印。

+0

您刚才复制的MSDN示例代码。其中警告:“此代码仅适用于可检测和打印纯文本的打印机,其中一些打印机不能。”把“一些”变成“最”,而不是大量的点阵式打印机了。不要这样做,请改用PrintDocument。 –

+0

请检查此http://stackoverflow.com/a/6106155/1849024 – imsome1

+0

@HansPassant感谢您的回复。此代码是否适用于POS热敏打印机,或者我也必须使用PrintDocument? –

回答

0

正如Tony在评论中指出的那样,JobStream在.NET Framework 4.5 for Windows 8中发生了变化,并且使用XPS文档格式。如果你想打印,你将不得不遵循指导原则。

我还没有找到一个可行的解决方案,但你可以尝试使用XPS Printing API

最接近的解决方案,我发现是herehere由微软here提到