Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

One possible method to conceal the Download dialog box in WebView2 during the process of downloading page content or using the "Save as PDF" feature is to use the WebView2 Environment.Options object and set the silent printing and download options to true. This will enable the silent download and printing mode and hide the download dialog box.

For example:

var env = await CoreWebView2Environment.CreateAsync();
var options = new CoreWebView2EnvironmentOptions();
options.AdditionalBrowserArguments = "--silent-printing --disable-downloads";
env.CreateCoreWebView2Async(null, new SilentDownloadWebView2Handler());

public class SilentDownloadWebView2Handler : CoreWebView2CreateCoreWebView2EnvironmentCompletedHandler {

    public override void CreateCoreWebView2EnvironmentCompleted(int result, CoreWebView2Environment env) {
        var options = env.Options;
        options.SilentPrintingEnabled = true;
        options.IsStatusBarEnabled = false;
        options.AreDefaultContextMenusEnabled = true;
        options.AreDevToolsEnabled = false;
        options.IsZoomControlEnabled = false;
        options.DownloadStarting += (sender, e) => e.Cancel = true;
        base.CreateCoreWebView2EnvironmentCompleted(result, env);
    }
}

This code will create a WebView2 environment with the silent printing and download options enabled, and a custom event handler that cancels any download requests. This will prevent the download dialog box from appearing and allow the page content to be downloaded or saved as PDF without any user intervention.