0

On click of a button, I am trying to print the HTML page

enter image description here

I think the problem is in the path? Without hardcoding how should I print this document, currently it just opens the printer tray and prints This page cant be displayed.

help.html

<html>
<head>
<body>
This is my testing...
<body>
</head>
</html>

Here is my C# code

   private void button1_Click(object sender, EventArgs e)
        {
            // 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(@"D:\PMO Project\PMO\WindowsFormsApplication1\WindowsFormsApplication1\HTML\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();
        }

Screenshot of what is printed

enter image description here

user580950
  • 3,558
  • 12
  • 49
  • 94
  • Can you show us a screenshot of the "printer tray"? Or do you mean that the physical paper try on the physical printer is popping out? – 15ee8f99-57ff-4f92-890c-b56153 Mar 20 '17 at 19:23
  • @EdPlunkett I just checked the printer prints the page but its not the html page that it is printing (doenst print help.html) but it prints the page cant be displayed res://ieframe.dll/dnserrordiaoff.htm – user580950 Mar 20 '17 at 19:25
  • Is your help.html valid? Did you try to open it in browser? – Roman Marusyk Mar 20 '17 at 19:26
  • @MegaTron question updated with html – user580950 Mar 20 '17 at 19:28
  • try attaching your webbrowser control to the form (you have a window application with one or more form, ain't it?), and check you can load the html. Possibly you are even loading your page, meaning the problem is before the printing – Gian Paolo Mar 20 '17 at 19:32
  • 1
    I like the hardcoded path to your project directory. It's the little details like that -- or posting a question about why it's not printing without first walking over to the printer to see if any paper came out -- which really bring a question to life. But never mind me, I'm a cruel and whimsical old man. I suggest you *temporarily* put a WebBrowser control in your UI where you can see it, and figure out how to compile the HTML file *as a resource* and load it in the WebBrowswer. Once you can see it on the screen, then work on printing it. Then you'll be able to make this work. – 15ee8f99-57ff-4f92-890c-b56153 Mar 20 '17 at 19:34
  • http://stackoverflow.com/questions/7194851/load-local-html-file-in-a-c-sharp-webbrowser – apocalypse Mar 20 '17 at 19:52

0 Answers0