使用虚拟打印机时如何检查打印是否完成?

发布于 2024-09-27 02:37:44 字数 1220 浏览 9 评论 0原文

我正在使用虚拟打印机在 C# 程序中将 Word 文档打印为图像文件。到目前为止,一切都很顺利,除了我不知道打印过程何时完成,以便我可以读取生成的图像的内容。 这是我的代码:

using System;
using Microsoft.Office.Interop.Word;
using Word=Microsoft.Office.Interop.Word;

var app = new ApplicationClass();
object filename = "C:\\ad.doc";
var missing = Type.Missing;
var doc = app.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
var oldPrinter = app.ActivePrinter;
app.ActivePrinter = "Name of printer";
object outputFileName = "c:\\ad.tif";
object trueValue = true;
object falseValue = false;

doc.PrintOut(ref trueValue, ref falseValue, ref missing, ref outputFileName, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref trueValue, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref missing);


app.ActivePrinter=oldPrinter ;                
doc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);

那么我如何确定打印处理已完成,以便我可以继续并获取图像内容?

I am using a virtual printer to print a word document into an image file in a C# program. So far everything is going fine except that I don't know when the printing process is finished so I can read the content of the generated image.
Here's my code :

using System;
using Microsoft.Office.Interop.Word;
using Word=Microsoft.Office.Interop.Word;

var app = new ApplicationClass();
object filename = "C:\\ad.doc";
var missing = Type.Missing;
var doc = app.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
var oldPrinter = app.ActivePrinter;
app.ActivePrinter = "Name of printer";
object outputFileName = "c:\\ad.tif";
object trueValue = true;
object falseValue = false;

doc.PrintOut(ref trueValue, ref falseValue, ref missing, ref outputFileName, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref trueValue, ref missing, ref missing,
                             ref missing, ref missing, ref missing, ref missing, ref missing);


app.ActivePrinter=oldPrinter ;                
doc.Close(ref missing, ref missing, ref missing);
app.Quit(ref missing, ref missing, ref missing);

Then how can I be sure that the print processing is finished so I can continue and get the image content?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

口干舌燥 2024-10-04 02:37:44

不幸的是,我发现检查 Word 中的打印状态的唯一方法是以下两种方法之一。

  1. 同步打印。但不太好,因为它会把你吊起来
    直至打印完成。
  2. 异步打印文档,然后检查
    循环中或循环中的 APPLICATION.BACKGROUNDPRINTINGSTATUS 属性
    后台工作线程不断,直到变成0(不再
    打印)或者你遇到了看门狗超时

类似这样的事情......

        Do Until _Doc.Application.BackgroundPrintingStatus = 0
            System.Windows.Forms.Application.DoEvents()
            System.Threading.Thread.Sleep(750)
        Loop

不完美,但它有效。

请注意,这只会告诉您何时完成从 word 的假脱机。如果您谈论的是了解文档何时实际完成在打印机上打印,那就是另一个问题了。您将需要打印作业ID,并且必须查询打印机后台处理程序的内容,但我无法帮助您。

Unfortunately, about the only way I've found to check for printing status in word is one of two things.

  1. Print synchronously. Not great though because it'll can hang you
    till the printing is complete.
  2. Print the doc asynchronously, and then check the
    APPLICATION.BACKGROUNDPRINTINGSTATUS property in a loop or on a
    background worker thread continuously until it becomes 0 (no longer
    printing) or you hit a watchdog timeout

Something like this....

        Do Until _Doc.Application.BackgroundPrintingStatus = 0
            System.Windows.Forms.Application.DoEvents()
            System.Threading.Thread.Sleep(750)
        Loop

Not perfect, but it works.

Note that this will only tell you when its finished spooling from word. If you're talking about knowing when the document is actually completed PRINTING on the printer, that's a whole other issue. You'll need the print jobID and have to query the printer spooler stuff, which I couldn't help you on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文