WinForm打印html字符串
思路
使用WebBrowser加载html内容,输出成Image进行打印。
代码
var print = new PrintDocument();
print.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
print.DefaultPageSettings.Landscape = landscape;
print.PrintPage += (o, e) =>
{
var img = new Bitmap(e.PageBounds.Width, e.PageBounds.Height);
var br = new WebBrowser();
br.ScrollBarsEnabled = false;
br.Width = e.PageBounds.Width;
br.Height = e.PageBounds.Height;
br.DocumentText = content;
br.Document.Write(content);
br.DrawToBitmap(img, e.PageBounds);
e.Graphics.DrawImage(img, e.PageBounds);
};
print.Print();