Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowser.Print() wait until complete. .NET

I am using a WebBrowser control in VB.NET and calling the Print() method. I am printing out using a PDF printer and when Print() is called it is not immediately kicked off (it waits until it has completed running code for the entire Sub or block.

I need to make sure the file I am printing too is complete and continue process with this file, therefore, I would like to print on demand and get some status of when the operation is complete. I have tried usign printDocument and process without luck.

Anyone have any ideas?

like image 853
Matt Avatar asked Mar 18 '10 13:03

Matt


2 Answers

Check out the PrintTemplateTeardown event of the underlying unmanaged WebBrowser object. Sometimes that event gets fired multiple times but hopefully this will point you in the right direction. You need to add a reference to Microsoft Internet Controls.

Private Sub Print()
    AddHandler DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser).PrintTemplateTeardown, AddressOf PrintDone
    WebBrowser1.Print()
End Sub
Private Sub PrintDone(ByVal obj As Object)
    Trace.WriteLine("printed")
    RemoveHandler DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser).PrintTemplateTeardown, AddressOf PrintDone
End Sub
like image 77
Chris Haas Avatar answered Oct 04 '22 23:10

Chris Haas


Your best bet is to get a handle on your 'printjobscollection' for your default printer and ensure that the jobcount = 0

like this in vb.net:

    Dim intprint As Integer = Nothing

    retry2:
    intprint = GetPrintJobsCollection(printerinuse)
    If Not intprint = 0 Then
        System.Threading.Thread.Sleep(1000)
        GoTo retry2
    End If
    'do what you want to do after print completes here
like image 38
jasondemon Avatar answered Oct 05 '22 01:10

jasondemon