开发者

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!

A开发者_运维问答ddendum:

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜