0

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. I got following error

An unhandled exception of type 'System.UriFormatException' occurred in System.dll

Additional information: Invalid URI: The format of the URI could not be determined.

Newwebbrowser.Navigate("test.html");//Here i got exception

Indu
  • 31
  • 5
  • wait until it is loaded completely, then you can print the page. Try [this](https://msdn.microsoft.com/en-us/library/b0wes9a3%28v=vs.85%29.aspx). – kennyzx Mar 18 '15 at 12:05
  • i have tried th solution which was suggested by Daniel. But I got an error. An unhandled exception of type 'System.UriFormatException' occurred in System.dll Additional information: Invalid URI: The format of the URI could not be determined.. Newwebbrowser.Navigate("test.html"); //Here i get exception – Indu Mar 18 '15 at 12:12
  • Where is located "test.html"? Read about the `URI` [schemas](https://msdn.microsoft.com/en-us/library/system.uri.scheme(v=vs.110).aspx). – denys-vega Mar 18 '15 at 14:57
  • html file is located at bin folder – Indu Mar 19 '15 at 05:47

1 Answers1

1

The error message means what it says -- it doesn't know what "test.html" means (or where it is).

Use "file:///c:\test\test.html" or "http://localhost/test.html" whatever is appropriate, for example:

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

Pete Cole
  • 26
  • 3