【问题标题】:How can I print a saved html file from a WPF application?如何从 WPF 应用程序打印保存的 html 文件?
【发布时间】:2015-03-18 11:59:46
【问题描述】:

我已经尝试如下。

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 文件已经在驱动器中。 我收到以下错误

System.dll 中出现“System.UriFormatException”类型的未处理异常

附加信息:无效的 URI:无法确定 URI 的格式。

Newwebbrowser.Navigate("test.html");//这里出现异常

【问题讨论】:

  • 等到完全加载后,就可以打印页面了。试试this
  • 我已经尝试过 Daniel 建议的解决方案。但我得到了一个错误。 System.dll 中出现“System.UriFormatException”类型的未处理异常附加信息:无效的 URI:无法确定 URI 的格式.. Newwebbrowser.Navigate("test.html"); //这里我得到异常
  • “test.html”在哪里?阅读URIschemas
  • html文件位于bin文件夹

标签: c# wpf


【解决方案1】:

错误消息的意思是它所说的——它不知道“test.html”是什么意思(或者它在哪里)。

使用“file:///c:\test\test.html”或“http://localhost/test.html”任何合适的,例如:

"file:///" + AppDomain.CurrentDomain.BaseDirectory + "test.html"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2011-06-11
    相关资源
    最近更新 更多