c# - PrintQueue.AddJob 方法不起作用

标签 c# printing dllimport spooler

我正在开发一个应用程序,我必须在原始打印文本后添加一行额外的文本。

为此,我使用 Print Spooler API 的 FindFirstPrinterChangeNotification 和 FindNextPrinterChangeNotification 方法,效果很好。

我能够获取打印队列,其中显示作业数 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();

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

最佳答案

正如 Tony 在评论中指出的那样,JobStream 在 Windows 8 及更高版本的 .NET Framework 4.5 中已更改为使用 XPS 文档格式。如果您想打印,则必须遵循指南。

我还没有找到有效的解决方案,但您可以尝试使用 XPS Printing API

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

关于c# - PrintQueue.AddJob 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42241600/

相关文章:

c# - C# 中对文本框的异步写入被覆盖

c# - 使用 FileSavePicker 在 Windows Phone 8.1 中保存图像

c# - 在 MSTest 中忽略从通用基类继承的测试类

visual-studio - 为什么 DLL 函数调用没有编译为相关的 CALL 指令

c# - 如何在 C# 中将 DllImport 与接口(interface)一起使用?

c# - 如何将本地资源文件作为依赖项传递到 MediaPlayer.Create 中?

php - 使用 PHP 打印多个文件

php - 标签打印机的 CSS

javascript - 打印时在表格中添加分页符

c# - 在 C# 应用程序中从非托管 DLL 导入函数