2017-02-15 144 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提到