如何打印WPF WebBrowser控件中显示的PDF文件 [英] How to print a PDF file displayed in the WPF WebBrowser control

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

问题描述

我有一个WPF应用程序,该应用程序使用标准的WebBrowser控件实现了简单的Web浏览器.当用户导航到PDF文档时,该文档将在MSIE的标准Adobe Reader插件的WebBrowser控件中内联显示.现在,我需要以编程方式打印文件.我该怎么办?

I have a WPF application that implements a simple web browser using the standard WebBrowser control. When the user navigates to a PDF document, the document gets displayed inline in the WebBrowser control in the standard Adobe Reader plugin for MSIE. Now I need to print the file programmatically. How do I do it?

我知道Adobe Reader具有带有打印命令的COM界面. MSIE插件中也提供此接口吗?如何从只能访问WebBrowser控件的WPF代码访问它?

I know that the Adobe Reader has a COM interface with the print command. Is this interface available in the MSIE plugin, too? How do I access it from the WPF code where I only have an access to the WebBrowser control?

感谢您的建议!

推荐答案

无论是HTML还是PDF,这都是在WPF WebBrowser控件中打印文档的方式:

This is how you print the document in the WPF WebBrowser control, no matter if it is HTML or PDF:

private void Print_Click(object sender, RoutedEventArgs e)
{
    // Try to print it as Html
    var doc = webBrowser.Document as IHTMLDocument2;
    if (doc != null)
    {
        doc.execCommand("Print", true, 0);
        return;
    }

    // Try to print it as PDF
    var pdfdoc = webBrowser.Document as AcroPDFLib.AcroPDF;
    if (pdfdoc != null)
    {
        pdfdoc.Print();
    }
}

对于PDF打印,您必须将AcroPDFLib添加到项目的引用中.

For PDF printing, you will have to add AcroPDFLib to your project's references.

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

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