Click here to Skip to main content
15,844,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi :)

I'm doing some work with HTML and I want to print (on paper) these HTML files, in reality, the file does not exist, everything is saved in a string, all text in HTML, but I would like to print, already formatted...

for example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
    class Program
        static void Main(string[] args)
            string HTML =
"<html>" +
"<head>" +
"    <style type=\"text/css\">" +
"    .title {" +
"        color: blue;" +
"        text-decoration: bold;" +
"        text-size: 1em;" +
"    }" +
"    .author {" +
"        color: gray;" +
"    }" +
"    </style>" +
"</head>" +
"<body>" +
"    <p>" +
"    <span class=\"title\">{0}</span>" +
"    <span class=\"author\">{1}</span>" +
"    </p>" +
"</body>" +

            // Just a sample of what I whant to do...
            // PseudoCode
            //Render the HTML code
            RenderHTML aa = new RenderHTML(string.Format(HTML, "Alexandre", "Bencz"));

I found that:[^]

But, I whant to know if have another way to do this, a more better way.. ? :)
Updated 20-Mar-23 18:09pm

You can use the WebBrowser control and print from it; it could be System.Windows.Forms.WebBrowser or System.Windows.Controls.WebBrowser. You know that.

But there is one very different and interesting opportunity: the control from this CodeProject article: A Professional HTML Renderer You Will Use[^].

This is a control of a very good quality which allow to render HTML in a lightweight way, using available System.Windows.Forms.RichTextBox control. You can render HTML and print its content.

Share this answer
try this.
You can use the WebBrowser control to do so. It will allow you to show HTML inside your WinForms.

The DocumentText proprety will allow you to set a String that represent the HTML you want to show.

For example:
webBrowser.DocumentText = "<html><body><p>I like StackOverflow</p><body></html>";

Afterward if you want to print the page, you'll have to wait until the Document is completed and call the Print method of the WebBrowser. MSDN shows an easy way to do it:
 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.

    // Dispose the WebBrowser now that the task is complete. 

You should also consider trying to use the method PrintDialog to make sure the issue is not your printing configuration.

Here is the link to MSDN: Print with a WebBrowser control on MSDN[^]

Possible duplicate: Printing WebBrowser control content[^]
Share this answer
You can call the print method in javascsript within the page, but to get this to print as a HTML page, you need to print it in the browser, otherwise it's just text.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900