Idioma

CoreWebView2.PrintToPdfAsync(String, CoreWebView2PrintSettings) Method

Definition

Print the current page to PDF asynchronously with the provided settings.

public System.Threading.Tasks.Task<bool> PrintToPdfAsync(string ResultFilePath, Microsoft.Web.WebView2.Core.CoreWebView2PrintSettings printSettings = default);
member this.PrintToPdfAsync : string * Microsoft.Web.WebView2.Core.CoreWebView2PrintSettings -> System.Threading.Tasks.Task<bool>
Public Function PrintToPdfAsync (ResultFilePath As String, Optional printSettings As CoreWebView2PrintSettings = Nothing) As Task(Of Boolean)

Parameters

ResultFilePath
String

Returns

Examples

CoreWebView2PrintSettings printSettings = null;
string orientationString = e.Parameter.ToString();
if (orientationString == "Landscape")
{
    printSettings = WebViewEnvironment.CreatePrintSettings();
    printSettings.Orientation =
        CoreWebView2PrintOrientation.Landscape;
}

Microsoft.Win32.SaveFileDialog saveFileDialog =
    new Microsoft.Win32.SaveFileDialog();
saveFileDialog.InitialDirectory = "C:\\";
saveFileDialog.Filter = "Pdf Files|*.pdf";
Nullable<bool> result = saveFileDialog.ShowDialog();
if (result == true)
{
    _isPrintToPdfInProgress = true;
    bool isSuccessful = await _iWebView2.CoreWebView2.PrintToPdfAsync(
        saveFileDialog.FileName, printSettings);
    _isPrintToPdfInProgress = false;
    string message = (isSuccessful) ?
        "Print to PDF succeeded" : "Print to PDF failed";
    MessageBox.Show(this, message, "Print To PDF Completed");
}

Remarks

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

Use resultFilePath to specify the path to the PDF file. The host should provide an absolute path, including file name. If the path points to an existing file, the file will be overwritten. If the path is not valid, the method fails with ArgumentException.

The async PrintToPdfAsync operation completes when the data has been written to the PDF file. At this time the awaitable Task<TResult> will complete and evaluate to false if printing failed. If the application exits before printing is complete, the file is not saved. Only one PrintToPdfAsync operation can be in progress at a time. If PrintToPdfAsync is called while a print to PDF operation is in progress, the Task<TResult> completes and returns false.

Applies to