问题
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