Partager via


CoreWebView2.PrintAsync(CoreWebView2PrintSettings) Method

Definition

Print the current web page asynchronously to the specified printer with the provided settings.

public System.Threading.Tasks.Task<Microsoft.Web.WebView2.Core.CoreWebView2PrintStatus> PrintAsync (Microsoft.Web.WebView2.Core.CoreWebView2PrintSettings printSettings);
member this.PrintAsync : Microsoft.Web.WebView2.Core.CoreWebView2PrintSettings -> System.Threading.Tasks.Task<Microsoft.Web.WebView2.Core.CoreWebView2PrintStatus>
Public Function PrintAsync (printSettings As CoreWebView2PrintSettings) As Task(Of CoreWebView2PrintStatus)

Parameters

Returns

Examples

// Function to get printer name by displaying printer text input dialog to the user.
// User has to specify the desired printer name by querying the installed printers list on the
// OS to print the web page.
// You may also choose to display printers list to the user and return user selected printer.
string GetPrinterName()
{
    string printerName = "";

    var dialog = new TextInputDialog(
                title: "Printer Name",
                description: "Specify a printer name from the installed printers list on the OS.",
                defaultInput: "");
    if (dialog.ShowDialog() == true)
    {
        printerName = dialog.Input.Text;
    }
    return printerName;

    // or
    //
    // Use GetPrintQueues() of LocalPrintServer from System.Printing to get list of locally installed printers.
    // Display the printer list to the user and get the desired printer to print.
    // Return the user selected printer name.
}

// Function to get print settings for the selected printer.
// You may also choose get the capabilities from the native printer API, display to the user to get
// the print settings for the current web page and for the selected printer.
CoreWebView2PrintSettings GetSelectedPrinterPrintSettings(string printerName)
{
    CoreWebView2PrintSettings printSettings = null;
    printSettings = WebViewEnvironment.CreatePrintSettings();
    printSettings.ShouldPrintBackgrounds = true;
    printSettings.ShouldPrintHeaderAndFooter = true;

    return printSettings;

    // or
    //
    // Get PrintQueue for the selected printer and use GetPrintCapabilities() of PrintQueue from System.Printing
    // to get the capabilities of the selected printer.
    // Display the printer capabilities to the user along with the page settings.
    // Return the user selected settings.
}

// This example prints the current web page to the specified printer with the settings.
async void PrintToPrinter()
{
    string printerName = GetPrinterName();
    CoreWebView2PrintSettings printSettings = GetSelectedPrinterPrintSettings(printerName);
    string title = _iWebView2.CoreWebView2.DocumentTitle;
    try
    {
        CoreWebView2PrintStatus printStatus = await _iWebView2.CoreWebView2.PrintAsync(printSettings);

        if (printStatus == CoreWebView2PrintStatus.Succeeded)
        {
            MessageBox.Show(this, "Printing " + title + " document to printer is succeeded", "Print to printer");
        }
        else if (printStatus == CoreWebView2PrintStatus.PrinterUnavailable)
        {
            MessageBox.Show(this, "Selected printer is not found, not available, offline or error state", "Print to printer");
        }
        else
        {
            MessageBox.Show(this, "Printing " + title + " document to printer is failed",
                "Print");
        }
    }
    catch (ArgumentException)
    {
        MessageBox.Show(this, "Invalid settings provided for the specified printer",
            "Print");
    }
    catch (Exception)
    {
        MessageBox.Show(this, "Printing " + title + " document already in progress",
                "Print");

    }
}

Remarks

See @CoreWebView2PrintSettings for description of settings. Passing null for `printSettings` results in default print settings used.

The method will return @CoreWebView2PrintStatus as @CoreWebView2PrintStatus$.PrinterUnavailable if `printerName` doesn't match with the name of any installed printers on the user OS. The method will throw ArgumentException if the caller provides invalid settings for a given printer.

The async Print operation completes when it finishes printing to the printer. Only one `Printing` operation can be in progress at a time. If `Print` is called while a PrintAsync(CoreWebView2PrintSettings) or PrintToPdfAsync(String, CoreWebView2PrintSettings) or PrintToPdfStreamAsync(CoreWebView2PrintSettings) job is in progress, the IAsyncOperation throws exception. This is only for printing operation on one webview.

| Error | PrintStatus | Notes | | --- | --- | --- | | No | CoreWebView2PrintStatus.Succeeded | Print operation succeeded. | | No | CoreWebView2PrintStatus.PrinterUnavailable | If specified printer is not found or printer status is not available, offline or error state. | | No | CoreWebView2PrintStatus.OtherError | Print operation is failed. | | ArgumentException | CoreWebView2PrintStatus.OtherError | If the caller provides invalid settings for the specified printer. | | Exception | CoreWebView2PrintStatus.OtherError | Print operation is failed as printing job already in progress. |

Applies to