.netwpfwebbrowser-controlchromium-embeddedwebview2

Print functionality in WebView2 control


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.


Solution

  • (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.