开发者

When using a virtual printer how can I check that print is finished?

开发者 https://www.devze.com 2023-01-19 12:20 出处:网络
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 s开发者_高级运

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 s开发者_高级运维o 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消