0

I'm working on a print app that needs to print the images sequentially. The list has tif images and xml formatted documents. I'm using WebBrowser to navigate to the xml and after DocumentComplete I call WebBrowser.Print() to start printing.

The problem is that WebBrowser doesn't starting print immediately. It looks like it's sporadic when WebBrowser actually wants to print.

Here is the code that is similar how my code looks like.

Anyone know what triggers the printing to start or away work around it?

public partial class Form1 : Form
{
    private WebBrowser _webBrowser1 = new WebBrowser();
    private List<string> _list = new List<string>();

    public Form1()
    {
        InitializeComponent();

        this._webBrowser1.Name = "webBrowser1";
        this._webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);

        this._list.Add("test1.tif");
        this._list.Add("http://bing.com");
        this._list.Add("test2.tif");
        this._list.Add("test3.tif");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.StartPrinting();
    }

    private void StartPrinting()
    {
        if (this._list.Count == 0)
            return;

        string imgPath = this._list[0];
        this._list.RemoveAt(0);

        if (imgPath.EndsWith(".tif") == true)
        {
            // Code to print tif
            System.Threading.Thread.Sleep(3000);

            // Print Next Image
            this.StartPrinting();
        }
        else
        {
            // ex. http://test.com/mytest.xml
            this._webBrowser1.Navigate(imgPath);
        }
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        // THIS IS THE PROBLEM, it doesn't show the prompt until later in the loop
        this._webBrowser1.ShowPrintDialog();

        // Print Next Image
        this.StartPrinting();
    }
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • do you see this.StartPrinting(); at the end of print? It calls itself until it runs out of links in the List. –  May 05 '14 at 14:02

1 Answers1

0

WebBrowser printing is asynchronous. There's PrintTemplateTeardown event which is fired by the underlying WebBrowser ActiveX control when the spooling part of the printing has finished. I posted an example illustrating how to handle it, so it's possible to print a series of URLs asynchronously.

Alternatively, you can force printing (spooling) to complete synchronously with PRINT_WAITFORCOMPLETION flag:

const int OLECMDID_PRINT = 6;
const int OLECMDEXECOPT_DONTPROMPTUSER = 2;
const int PRINT_WAITFORCOMPLETION = 2;

dynamic webBrowserAx = webBrowser.ActiveXInstance;
webBrowserAx.ExecWB(
    OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION);

Note this will block the UI thread until spooling has finished.

More on WebBrowser/MSHTML printing: https://stackoverflow.com/a/19172580/1768303.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486