Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print html in C#

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?

like image 796
MoShe Avatar asked Oct 06 '11 15:10

MoShe


People also ask

Can I use HTML in C?

The C compiler is designed to be able to extract and compile C code embedded within HTML files. This capability means that C code can be written to be displayed within a browser utilizing the full formatting and display capability of HTML.

How can you print for in C?

You can print all of the normal C types with printf by using different placeholders: int (integer values) uses %d. float (floating point values) uses %f. char (single character values) uses %c.

How do you display HTML code?

You can include code examples in your HTML by using the <code> tag. This automatically uses a monospaced font and also semantically labels our code as what it is.


1 Answers

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

like image 116
JohnFx Avatar answered Oct 13 '22 00:10

JohnFx