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

4
  • wait until it is loaded completely, then you can print the page. Try this. Commented Mar 18, 2015 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 Commented Mar 18, 2015 at 12:12
  • Where is located "test.html"? Read about the URI schemas. Commented Mar 18, 2015 at 14:57
  • html file is located at bin folder Commented Mar 19, 2015 at 5:47

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.