从WebBrowser控件在横向模式下打印?

时间:2011-02-11 14:51:58

标签: c# .net winforms printing webbrowser-control

System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();

wb.DocumentStream = new FileStream("C:\a.html", FileMode.Open, FileAccess.Read);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
    Application.DoEvents();
}
wb.Print();

我知道如何从PrinterDocument对象设置页面方向,但不知道如何从WebBrowser对象设置页面方向。有什么办法吗?谢谢!

1 个答案:

答案 0 :(得分:4)

首先,我建议您使用异步事件模型:

wb.DocumentCompleted += wb_DocumentCompleted;

private void wb_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
{
    ((WebBrowser)sender).Print();
}

要打印(添加对Microsoft.mshtml.dll的引用):

mshtml.IHTMLDocument2 doc = wb.Document.DomDocument as mshtml.IHTMLDocument2;
doc.execCommand("print", showUI, templatePath);

请参阅IHTMLDocument2.execCommandMSDN forum question并点击链接。