Reputation: 1001
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.
Upvotes: 10
Views: 12419
Reputation: 4377
(Update 2023-06-02: Linking to WebView2 printing doc based on this answer. Also many APIs are no longer experimental)
(Update 2022-11-30: Since I last replied here, we've added additional print methods I describe below)
There are a few different ways to print which give you varying levels of ease of use and control:
webview2.CoreWebView2.ShowPrintUI();
// null for default print settings.
await webview2.CoreWebView.PrintAsync(null);
// null for default print settings.
var stream = await webview.CoreWebView2.PrintToPdfStreamAsync(null);
await webview2.CoreWebView2.ExecuteScriptAsync("window.print();");
See the WebView2 printing doc for more details.
See also the old Print feature request GitHub thread.
Upvotes: 20
Reputation: 716
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.
Upvotes: 1
Reputation: 16701
WebView2 supports printing to PDF with PrintToPdfAsync(path, settings)
:
await webView2.CoreWebView2.PrintToPdfAsync(@"Path/To/File.pdf");
Upvotes: 4