How to print html in C#
Asked Answered
K

2

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?

Kasey answered 6/10, 2011 at 15:37 Comment(2)
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 (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.Fiji
windows form but it is not metterKasey
L
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

Laveralavergne answered 6/10, 2011 at 15:40 Comment(1)
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.Insincerity
F
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();
            }
        }
}
Fiddlestick answered 4/8, 2020 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.