[英]How can I print the contents of a WebBrowser control?
我在这里找到了像这样使用的代码:
printDialog1.PrintDocument
(((IDocumentPaginatorSource)webBrowserMap.Document).DocumentPaginator, "Alchemy Map");
...但是我得到了两个错误信息,分别是:
0)'System.Windows.Forms.PrintDialog' does not contain a definition for 'PrintDocument' and no extension method 'PrintDocument' accepting a first argument of type 'System.Windows.Forms.PrintDialog' could be found (are you missing a using directive or an assembly reference?)
1) The type or namespace name 'IDocumentPaginatorSource' could not be found (are you missing a using directive or an assembly reference?)
然后,我尝试通过这种方式从此处修改代码:
private void buttonPrint_Click(object sender, EventArgs e)
{
WebBrowser wb = getCurrentBrowser();
wb.ShowPrintDialog();
}
private WebBrowser getCurrentBrowser()
{
return (WebBrowser)tabControlAlchemyMaps.SelectedTab.Controls[0];
}
...但是得到了“ 未处理System.InvalidCastException HResult = -2147467262消息=无法将类型为“ System.Windows.Forms.Button”的对象转换为类型为“ System.Windows.Forms.WebBrowser”。
然后,我尝试使用以下代码从此处找到的内容派生:
private void buttonPrint_Click(object sender, EventArgs e)
{
WebBrowser web = getCurrentBrowser();
web.ShowPrintDialog();
}
private WebBrowser getCurrentBrowser()
{
// I know there's a better way, because there is only one WebBrowser control on the tab
foreach (var wb in tabControlAlchemyMaps.SelectedTab.Controls.OfType<WebBrowser>())
{
return wb;
}
return null;
}
...并且,尽管我可以逐步执行,并且似乎可以正常运行(我进入“ web.ShowPrintDialog()”行,但未显示任何错误消息),但看不到任何打印对话框。 所以,我怎么能去打印WebBrowser控件的内容是什么?
有关打印Windows窗体Web浏览器控件的MSDN页面: http : //msdn.microsoft.com/zh-cn/library/b0wes9a3(v=vs.90).aspx
private void PrintHelpPage()
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");
}
private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.