Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# HTML printing HTML5
Hi Smile | :)
 
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>" +
"</html>";
 
            // Just a sample of what I whant to do...
            // PseudoCode
            //Render the HTML code
            RenderHTML aa = new RenderHTML(string.Format(HTML, "Alexandre", "Bencz"));
            aa.PrintDocumentInPaper();
        }
    }
}
 
I found that: http://msdn.microsoft.com/en-us/library/w290k23d.aspx[^]
 
But, I whant to know if have another way to do this, a more better way.. ? Smile | :)
Posted 1-Jan-13 13:31pm
Rate this: bad
good
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.
 
—SA
  Permalink  
Rate this: bad
good
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.
    ((WebBrowser)sender).Print();
 
    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}
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[^]
  Permalink  
Rate this: bad
good
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.
  Permalink  

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



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