Displaying Print Preview of HTML Document without DocumentViewer

前端 未结 1 1828
北海茫月
北海茫月 2021-02-10 17:05

I have a C#/WPF application in which I need to display a print preview for an HTML document -- essentially just like what one would see if looking at a print preview in Firefox

1条回答
  •  -上瘾入骨i
    2021-02-10 18:02

    You might want to use the WebBrowser control and extend it using the example provided here: http://www.codeproject.com/KB/miscctrl/wbp.aspx

    [Edit: updated the answer to illustrate how to accomplish the same using the WPF WebBrowser control (System.Windows.Controls.WebBrowser)]

    The underlying control is the same - it's the ActiveX component in SHDocVw.dll.

    I pulled together some better reference URL's for you. Turns out there's a pretty good lead on doing something similar from the MSDN documentation for the control: http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser(v=vs.90).aspx#4 There's also this: http://support.microsoft.com/kb/329014.

    You'll need to add a reference to SHDocVw, which is under the COM reference list as "Microsoft Internet Controls"

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
    internal interface IServiceProvider
    {
        [return: MarshalAs(UnmanagedType.IUnknown)]
        object QueryService(ref Guid guidService, ref Guid riid);
    }
    static readonly Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
    
    void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        IServiceProvider serviceProvider = null;
        if (webBrowser.Document != null)
        {
            serviceProvider = (IServiceProvider)webBrowser.Document;
        }
    
        Guid serviceGuid = SID_SWebBrowserApp;
        Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;
    
        object NullValue = null;
    
        SHDocVw.IWebBrowser2 target = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);
        target.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINTPREVIEW, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref NullValue, ref NullValue);
    }
    

    XAML:

    
        
            
                
                
            
        
    
    

    Anyone interested in the WinForms control version (System.Windows.Forms.WebBrowser) can skip all the IServiceProvider baggage and just use the ActiveXInstance property (which the WPF control doesn't expose:

    SHDocVw.WebBrowser target = webBrowser.ActiveXInstance as SHDocVw.WebBrowser;
    target.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINTPREVIEW, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, null, null);
    

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