10

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.

DotNetSpartan
  • 931
  • 4
  • 20
  • 41

3 Answers3

19

(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:

  • CoreWebView2.ShowPrintUI: Show the WebView2 print preview dialog or OS print dialog for the current top-level document in the WebView2. With this mechanism you easily get a printing experience the user will be familiar with. The end user chooses print settings via the print dialogs. (This method is stable and in and available in the release SDK since v1.0.1518.46).
webview2.CoreWebView2.ShowPrintUI();
  • CoreWebView2.PrintAsync: Silently print the current top-level document in the WebView2 using optional programmatically specified print settings. If you want to build your own print preview dialog or otherwise build your own print experience you can use this method. (This method is stable and available in the release SDK since v1.0.1518.46).
// null for default print settings.
await webview2.CoreWebView.PrintAsync(null);
  • CoreWebView2.PrintToPdfAsync/PrintToPdfStreamAsync: Silently print the current top-level document in the WebView2 to a PDF file or PDF stream. To completely control how printing is performed, you can print to a PDF and then build your own code to print the PDF. (The stream version of this method is stable and available in the release SDK since v1.0.1518.46).
// null for default print settings.
var stream = await webview.CoreWebView2.PrintToPdfStreamAsync(null);
  • Script injection: If you must support a WebView2 Runtime older than the above APIs, as a workaround for HTML documents you can inject script that calls the DOM print method. This has the limitations that it only works for HTML documents (not PDF for example) and only when script is enabled in the document. For example if in the following the webview2 is the WPF WebView2 class:
await webview2.CoreWebView2.ExecuteScriptAsync("window.print();");

See the WebView2 printing doc for more details.

See also the old Print feature request GitHub thread.

David Risney
  • 3,886
  • 15
  • 16
  • Similar issue posted on [GitHub here](https://github.com/microsoft/microsoft-ui-xaml/issues/2894) in case you get a chance/time for a comment/suggestion. – nam Jul 13 '20 at 16:56
  • @David Risney Is it possible to perform this printing "SILENT" to the default printer –  Dec 08 '21 at 10:45
  • Not that printing, but we did add a CoreWebView2.PrintToPdfAsync method you can use to create a PDF and then print that yourself however you like https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.printtopdfasync?view=webview2-dotnet-1.0.1054.31#Microsoft_Web_WebView2_Core_CoreWebView2_PrintToPdfAsync_System_String_Microsoft_Web_WebView2_Core_CoreWebView2PrintSettings_ – David Risney Dec 08 '21 at 21:38
  • @DavidRisney My problem is that I HAVE a PDF and need to print it to a specific printer. My app uses a shipping service. I do a REST request to get a label, and it comes back as PDF. Then, I need to scale and print it to a label printer. Injecting a script doesn't seem workable since this isn't HTML. – Jamie Jul 08 '22 at 19:15
  • 1
    There's additional print methods available that I've updated the answer with. Currently (2022-11-30) they are experimental but should be available in a release package at some time in the future. – David Risney Nov 30 '22 at 18:37
  • Hi. Any suggestions on how to use the custom printing? My problem is the standard print dialog is forced to browser control. – Andrew Truckle Dec 23 '22 at 18:13
  • What do you mean by custom printing? The options available are listed above. PrintAsync can take a PrintSettings and prints with no UI otherwise which is useful if you want to make your own print preview dialog. PrintToPdfAsync creates the printed content version of the website as a PDF that you can then do whatever you like with and otherwise completely control printing. – David Risney Jan 03 '23 at 17:41
4

WebView2 supports printing to PDF with PrintToPdfAsync(path, settings):

await webView2.CoreWebView2.PrintToPdfAsync(@"Path/To/File.pdf");
haldo
  • 14,512
  • 5
  • 46
  • 52
  • Apparently though it doesn't support printing pdfs themselves (or least not with PrintAsync()). All I get is blank page. – Mike Cheel Jan 20 '23 at 20:49
1

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.

Velja Radenkovic
  • 716
  • 1
  • 6
  • 27