[英]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.