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

2023-09-03 05:48:03 作者:刪除妳的記憶

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对象。没有办法做到这一点?谢谢!

I know how to set the page orientation from a PrinterDocument object, but not from a WebBrowser object. Any way to do this? Thanks!

推荐答案

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

First, I recommend you to use async event model:

wb.DocumentCompleted += wb_DocumentCompleted;

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

要打印(参考添加到 Microsoft.mshtml.dll ):

To print (add the reference to Microsoft.mshtml.dll):

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

请参阅 IHTMLDocument2.execCommand , MSDN论坛问题并跟踪链接。