0

我在这里找到了我这样使用的代码:

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 控件的内容呢?

4

1 回答 1

2

关于打印 windows 窗体 web 浏览器控件的 MSDN 页面:http: //msdn.microsoft.com/en-us/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();
}
于 2014-10-05T19:44:17.410 回答