2

we are currently using a winforms WebBrowser control in our app in a WindowsFormsHost and printing by calling 'WebBrowser.ShowPrintDialog()' We have an issue with this in that the dialog does not appear to be modal and the parent window can be dismissed causing issues if a print is later attempted.

I was looking at the new wpf webbrowser control in the hope that they will have fixed this issue when doing a similar thing themselves, but can find no way to print from it..

I found someone online talking of doing this:

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

but this throws an exception as the WebBrowser.Document does not support the IDocumentPaginatorSource interface.

Is there any way I can print from the wpf web browser control?

thanks

Trev
  • 174
  • 3
  • 10

1 Answers1

4

I am using this, and it works:

    mshtml.IHTMLDocument2 doc = webBrowser.Document as mshtml.IHTMLDocument2;
    doc.execCommand("Print", true, null);
Botz3000
  • 39,020
  • 8
  • 103
  • 127
  • thanks, that worked. Unfortunately I still have the problem where I can interact with (and dismiss) the UI that initiates the print even though the print dialog is still there. Do you have this issue? Any ideas on how to get an event when the print dialog is closed? – Trev Apr 29 '09 at 15:12
  • I don't know, but i'm pretty sure the print dialog is initiated from unmanaged code so that it might need some more work to listen for events like that. – Botz3000 Apr 29 '09 at 15:37
  • I hate you both, where are the using statements, how are you instantiating your webbrowser? – Martin Clemens Bloch Mar 05 '15 at 16:51
  • @MartinClemensBloch I don't have the original source anymore, but the WebBrowser is a control from Windows Forms (which is instantiated like other WinForms controls), and i think mshtml.IHTMLDocument2 is available when you reference mshtml.dll as a COM component. – Botz3000 Mar 06 '15 at 07:51
  • Yeah I figured it out, thanks though :) It didn't work though - its printing empty pages, but I'm printing now with WPF. I think for both Forms and WPF webbrowser it is required that you actually have the window. Instantiating the webbrowser in a code library only leads to empty pages printed. – Martin Clemens Bloch Mar 06 '15 at 09:13