Print functionality in WebView2 control

Viewed 4979

Thanks in Advance !

In my application, I have embedded a WebView2 control inside a WPF usercontrol.

Is there anyway or a workaround that can help me to take a print of the WebView2 control ?

I identified that the current WebView2 pre-release SDK provided by Microsoft does'nt have any thing specific to print functionality.

3 Answers

As a workaround for HTML documents you can inject script that calls the DOM print method. For example if in the following the webview2 is the WPF WebView2 class:

await webview2.CoreWebView2.ExecuteScriptAsync("window.print();");

Otherwise, we have some feature requests regarding printing in WebView2 Feedback, like this:

Controlling the print dialog/window

I was able to achieve printing in Windows Forms app by sending a print shortcut keys to the focused WebView2. I was using SendKeys class which is unfortunately not available in WPF, but there seems to be a workaround mentioned here.

After you figure out that part, its as simple as:

this.webView1.Focus();
SendKeys.Send("^p");

It works beautifully, displaying preview and setup inside WebView2.

Related