Click here to Skip to main content
13,291,257 members (62,541 online)
Rate this:
Please Sign up or sign in to vote.
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.. ? :)
Posted 1-Jan-13 14:31pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100