How to print a PDF file displayed in the WPF WebBrowser control

一笑奈何 提交于 2019-12-08 12:38:47

问题


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?

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?

Thanks for your suggestions!


回答1:


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();
    }
}

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



来源:https://stackoverflow.com/questions/16057446/how-to-print-a-pdf-file-displayed-in-the-wpf-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!