1

I tried to print formatted HTML using WebBrowser class. After the print, I want to close the application. If I tried to use close the application the printing is not working. I tried using the timer also nothing works.

Please find the code below.

static void Main(string[] args) {
 var b = new Program();
 string appPath = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
 b.runBrowserThread("file://" + appPath + "/receipt.html");

}

private void runBrowserThread(string url) {
 var th = new Thread(() => {
  var br = new WebBrowser();
  br.DocumentCompleted += browser_DocumentCompleted;
  br.Navigate(url);
  Application.Run();
 });
 th.SetApartmentState(ApartmentState.STA);
 th.Start();
}

void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
 var br = sender as WebBrowser;
 br.Print();
 //Application.ExitThread();
 Environment.Exit(0);
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sarath Kumar
  • 1,136
  • 1
  • 18
  • 41
  • [PrintTemplateTeardown](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa768296(v%3Dvs.85)) is what you are looking for. – Reza Aghaei Aug 04 '19 at 18:10
  • I've answered a similar question previously: https://stackoverflow.com/a/19737374/1768303 – noseratio Aug 05 '19 at 20:05

1 Answers1

5

PrintTemplateTeardown is what you are looking for. You can add a reference to SHDocVw. Then you have access to interfaces like IWebBrowser2 and DWebBrowserEvents2_Event.

You can find SHDocVw as "Microsoft Internet Controls" in COM tab of Reference manager window.

You can subscribe DocumentCompleted event to know when the file/url load completed. You can print html document without showing print dialog by calling IWebBrowser2.ExecWB. Also you can subscribe to DWebBrowserEvents2_Event.PrintTemplateTeardown to find out when print completed so you can close the application:

using System;
using System.Windows.Forms;
using SHDocVw;
class Program
{
    static System.Windows.Forms.WebBrowser browser;
    [STAThread]
    static void Main()
    {
        var fileName = "http://google.com";
        browser = new System.Windows.Forms.WebBrowser();
        browser.ScriptErrorsSuppressed = true;
        browser.DocumentCompleted += browser_DocumentCompleted;
        browser.Navigate(fileName);
        Application.Run();
    }
    private static void browser_DocumentCompleted(object sender, 
        WebBrowserDocumentCompletedEventArgs e)
    {
        var iwb2 = (IWebBrowser2)browser.ActiveXInstance;
        var events = (DWebBrowserEvents2_Event)browser.ActiveXInstance;
        events.PrintTemplateTeardown += browser_PrintTemplateTeardown;
        var missing = Type.Missing;
        iwb2.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, 
        ref missing, ref missing);
    }
    private static void browser_PrintTemplateTeardown(object pDisp)
    {
        browser.Dispose();
        Application.Exit();
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • This works!. I tried to print receipt but it leaving extra space and page number url everything. is there anyway we can remove it? – Sarath Kumar Aug 05 '19 at 07:06
  • I can suggest two options. If you want to continue using html printing, try to create simple html out put or use printer friendly styles (using media queries.) If using RDLC is an option for you, you can use design RDLC report and to print, take a look at [this post](https://stackoverflow.com/q/34727037/3110834). – Reza Aghaei Aug 05 '19 at 07:11
  • I'm designed in html only the design looks good. I have written couple of print queries also but still it's printing page number and leaving extra white space in the bottom – Sarath Kumar Aug 05 '19 at 07:35
  • 1
    Just to be clearer, we need the HTML content (and styles) as [MCVE]. Also since it doesn't have anything to do with current post, I think it's better to post a new Question. Feel free to notify me if you asked a new question, I'll give it a look. – Reza Aghaei Aug 05 '19 at 07:41