Printing WPF Web Browser Content

I am trying to print the content of a WPF WebBrowser control so that the print dialog does not appear, but I have no luck.

I've tried the following and I'm sure it actually worked:

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

      

but for some reason I am getting the following exception:

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

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

+2


source to share


2 answers


// 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

+6


source


For silent printing without dialogs use this code:

private void PrintCurrentPage()
{
    // document must be loaded for this to work
    IOleServiceProvider sp = WebBrowser1.Document as IOleServiceProvider;
    if (sp != null)
    {
        Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
        Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");
        const int OLECMDID_PRINT = 6;
        const int OLECMDEXECOPT_DONTPROMPTUSER = 2;

        dynamic wb; // should be of IWebBrowser2 type
        sp.QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, out wb);
        if (wb != null)
        {
            // this will send to the default printer
            wb.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, null, null);
        }
    }
}
[ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IOleServiceProvider
{
    [PreserveSig]
    int QueryService([MarshalAs(UnmanagedType.LPStruct)] Guid guidService, [MarshalAs(UnmanagedType.LPStruct)]  Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
}

      



Silent Print WebBrowser

+2


source







All Articles