sooprise
sooprise

Reputation: 23187

Printing An HTML String Without Creating An HTML File?

How can I print an HTML string so all of the HTML tags are recognized and rendered correctly? I imagine it's possible to create a .HTML file and print it, but if there is a way to do this without creating extra files I'd be interested in learning how. Thanks!

Addendum:

pd.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
pd.Print();

More code:

static private void PrintDocument_PrintPage(Object sender, PrintPageEventArgs e) {
    Font printFont = new Font("Courier New", 12);
    e.Graphics.DrawString("<b>Hello</b> world", printFont, Brushes.Black, 0, 0);
}

Printed result:

<b>Hello</b> world

Upvotes: 3

Views: 7512

Answers (1)

Oded
Oded

Reputation: 499002

The Graphics object does not understand HTML, and DrawString will do exactly as requested, as you have found out.

You will need to use the Graphics object with a bold font for Hello and a non bold font for world and remove the HTML markup.

So, for a more general approach, you would need an HTML parser (such as the HTML Agility Pack) and a way to translate the HTML to different fonts.

You may find it easier to use a WebBrowser control and use it to print.

Upvotes: 4

Related Questions