6

I would like to print a file using PrintDocument in C#. The file is simple HTML (I need it because I need the text in the file to be located in specific places within the page.)

My question is, how do I print the file so it will not print the HTML itself (tags, etc.) but the HTML as it would show in a web browser?

JYelton
  • 35,664
  • 27
  • 132
  • 191
MoShe
  • 6,197
  • 17
  • 51
  • 77
  • uhhhhhhhhhhhhhhhhhhhhhhhh. My spider sense is tingling. Would launching a webpage with the users default browser (or portable firefox) be an option? Otherwise you are force with using IE. Also i used an IE control one time. I had it browse a well known website and it happen to have malware ads that day (http://www.google.com/safebrowsing/diagnostic?site=thesite.com says in 13kpages only one has malware so its pretty rare) and unfortunately since IE6 was installed i got the virus and i was really annoyed. –  Oct 06 '11 at 16:03
  • windows form but it is not metter – MoShe Oct 06 '11 at 17:01

2 Answers2

11

Use a web browser control and call the print method on it like so:

private void PrintHelpPage()
{
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser();

    // Add an event handler that prints the document after it loads.
    webBrowserForPrinting.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(PrintDocument);

    // Set the Url property to load the document.
    webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");
}

private void PrintDocument(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    // Print the document now that it is fully loaded.
    ((WebBrowser)sender).Print();

    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}

MSDN Article on doing this

JohnFx
  • 34,542
  • 18
  • 104
  • 162
  • But what do we do when we need to print to a network printer? If I call the browser print method, it's not going to select an arbitrary printer I need. – Katastic Voyage Dec 14 '16 at 20:06
0

Use this method to successfully print the HTML using as there is a bug that will cause the DocumentCompleted Event to trigger multiple times. I have got an easy solution for it -

private class yourClassName
{
        WebBrowser webBrowserForPrinting;
        public void YourForm_Load()
        {
             webBrowserForPrinting = new webBrowserForPrinting();
             // Set the Url property to load the document.
             webBrowserForPrinting.Url = new Uri("Your HTML File Directory");
             webBrowserForPrinting.DocumentCompleted += WebBrowserForPrinting_DocumentCompleted;
        }

        int i = 0;
        private void WebBrowserForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            i++;
            if (i == 1)
            {
                ((WebBrowser)sender).ShowPrintPreviewDialog();
            }

            else
            {
                ((WebBrowser)sender).Dispose();
            }
        }
}

This method will show you the printing dialog as well with preview. If you don't want the preview use this one instead -

private class yourClassName
{
        WebBrowser webBrowserForPrinting;
        public void YourForm_Load()
        {
             webBrowserForPrinting = new webBrowserForPrinting();
             // Set the Url property to load the document.
             webBrowserForPrinting.Url = new Uri("Your HTML File Directory");
             webBrowserForPrinting.DocumentCompleted += WebBrowserForPrinting_DocumentCompleted;
        }

        int i = 0;
        private void WebBrowserForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            i++;
            if (i == 1)
            {
                ((WebBrowser)sender).ShowPrintDialog();
            }

            else
            {
                ((WebBrowser)sender).Dispose();
            }
        }
}