简体   繁体   English

如何从WPF应用程序中打印保存的html文件?

[英]How can I print a saved html file from a WPF application?

I have tried as follows. 我尝试如下。

private void btnPrint_Click(object sender, RoutedEventArgs e)
{
        Newwebbrowser.Navigate("test.html");
            IOleServiceProvider sp = Newwebbrowser.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; // will be of IWebBrowser2 type, but dynamic is cool
                sp.QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, out wb);
                if (wb != null)
                {
                    // note: this will send to the default printer, if any
                    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);
    }

Html file is already in the drive. HTML文件已在驱动器中。 I got following error 我收到以下错误

An unhandled exception of type 'System.UriFormatException' occurred in System.dll System.dll中发生了类型为'System.UriFormatException'的未处理的异常

Additional information: Invalid URI: The format of the URI could not be determined. 附加信息:无效的URI:无法确定URI的格式。

Newwebbrowser.Navigate("test.html");//Here i got exception Newwebbrowser.Navigate(“ test.html”); //在这里,我得到了例外

The error message means what it says -- it doesn't know what "test.html" means (or where it is). 错误消息表示其内容-不知道“ test.html”的含义(或位置)。

Use "file:///c:\\test\\test.html" or " http://localhost/test.html " whatever is appropriate, for example: 使用“ file:/// c:\\ test \\ test.html”或“ http://localhost/test.html ”,例如:

"file:///" + AppDomain.CurrentDomain.BaseDirectory + "test.html" “文件:///” + AppDomain.CurrentDomain.BaseDirectory +“ test.html”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从WPF应用程序中不受信任的安全站点下载文件? - How can I download a file from an untrusted secured site in a WPF application? 如何在WPF应用程序中播放.swf文件? - How can I play a .swf file on my WPF application? 如何获取保存的html文件而不显示原始html? - How can I get my saved html file to be rendered rather than displaying the raw html? 如何在应用程序的框架中使用视图? (WPF MVVM) - How can I use a view from a framework in an application? (WPF MVVM) 如何在wpf中打印文件? - How to Print File in wpf? 我需要一个WPF应用程序来填充HTML模板(理想情况下,就像MVC项目一样)。 我怎样才能做到这一点? - I need a WPF application to populate a HTML template (ideally like an MVC project can). How can I do this? 如何通过保存在 WPF 应用程序输出目录中的文件对话框使用上传的图像文件? - How to use uploaded image file via file dialog which is saved in the output directory on WPF Application? 在WPF中,如何从ToolBar继承并拥有XAML文件? - In WPF, how can I inherit from ToolBar AND have a XAML file? 如何为WPF应用程序修复此代码? - How can I fix this code for WPF Application? 如何将数据从 WPF 应用程序保存到文件? - How to save data from WPF application to file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM