打印WPF WebBrowser控件的内容 [英] Printing the contents of a WPF WebBrowser

查看:786
本文介绍了打印WPF WebBrowser控件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试打印,这样没有显示打印对话框一个WPF WebBrowser控件的内容,但我有没有运气。

我曾尝试以下,并相信它的工作:

  PrintDialog类PrintDialog类=新PrintDialog类();
printDialog.PrintDocument(((IDocumentPaginatorSource)browser.Document).DocumentPaginator,我的应用);

但由于某些原因,我现在得到以下异常:

*无法投类型的COM对象mshtml.HTMLDocumentClass接口类型System.Windows.Documents.IDocumentPaginatorSource。此操作失败,因为COM组件调用QueryInterface对IID接口{2C0C27DF-282F-3225-ADCD-CEC68F890EEB}'因以下错误失败:支持没有这样的接口(从HRESULT异常:0x80004002(E_NOINTERFACE)) *

我能想到已经改变了我的电脑上的唯一的事情是,我已经安装了IE8,因为我上次尝试这一点,但会真正打破它?


解决方案

  //发送打印命令到web浏览器。对于这个code的工作:添加Microsoft.mshtml .NET参考
mshtml.IHTMLDocument2 DOC = WebBrowser1.Document为mshtml.IHTMLDocument2;
doc.execCommand(打印,真实,NULL);

来源:
<一href=\"http://social.msdn.microsoft.com/Forums/en/wpf/thread/63ae5345-b2ca-45a9-9113-0ddc43e9925b\">http://social.msdn.microsoft.com/Forums/en/wpf/thread/63ae5345-b2ca-45a9-9113-0ddc43e9925b

I'm trying to print the contents of a WPF WebBrowser control so that no print dialog is shown, but am having no luck.

I have tried the following and am sure it did work:

PrintDialog printDialog = new PrintDialog();
printDialog.PrintDocument(((IDocumentPaginatorSource)browser.Document).DocumentPaginator, "My App");

but for some reason, I'm now getting the following exception:

*Unable to cast COM object of type 'mshtml.HTMLDocumentClass' to interface type 'System.Windows.Documents.IDocumentPaginatorSource'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{2C0C27DF-282F-3225-ADCD-CEC68F890EEB}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).*

The only thing I can think has changed on my PC is that I have installed IE8 since I last tried this, but would that really break it?

解决方案

// Send the Print command to WebBrowser. For this code to work: add the Microsoft.mshtml .NET reference
mshtml.IHTMLDocument2 doc = WebBrowser1.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, null);

Source: http://social.msdn.microsoft.com/Forums/en/wpf/thread/63ae5345-b2ca-45a9-9113-0ddc43e9925b

这篇关于打印WPF WebBrowser控件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆