Silently print HTML from WPF WebBrowser

后端 未结 1 2051
一整个雨季
一整个雨季 2021-01-22 01:34

I have to silently print HTML document from WebBrowser in WPF. I have done it NOT silently by that code:

mshtml.IHTMLDocument2 doc = WebBrowser1.Document as msht         


        
相关标签:
1条回答
  • 2021-01-22 02:17

    For silent printing with WPF WebBrowser, below code worked for me:

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

    Note: answer was inspired by this SO q & a

    0 讨论(0)
提交回复
热议问题