开发者

How to print html in C#

开发者 https://www.devze.com 2023-04-11 00:20 出处:网络
I would like to print a file using PrintDocument in C#. The file is simple HTML (I need it because I ne开发者_StackOverflow中文版ed the text in the file to be located in specific places within the pag

I would like to print a file using PrintDocument in C#. The file is simple HTML (I need it because I ne开发者_StackOverflow中文版ed 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?


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


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();
            }
        }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号