Compartilhar via


CoreWebView2.PrintToPdfStreamAsync(CoreWebView2PrintSettings) Method

Definition

Provides the Pdf data of current web page asynchronously for the provided settings.

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

Parameters

Returns

Examples

// This example prints the Pdf data of the current web page to a stream.
async void PrintToPdfStream()
{
    try
    {
        string title = _iWebView2.CoreWebView2.DocumentTitle;

        // Passing null for `PrintSettings` results in default print settings used.
        System.IO.Stream stream = await _iWebView2.CoreWebView2.PrintToPdfStreamAsync(null);
        DisplayPdfDataInPrintDialog(stream);
        MessageBox.Show(this, "Printing" + title + " document to PDF Stream " + ((stream != null) ? "succeeded" : "failed"), "Print To PDF Stream");
    }
    catch (Exception exception)
    {
        MessageBox.Show(this, "Printing to PDF Stream failed: " + exception.Message,
           "Print to PDF Stream");
    }
}

// Function to display current page pdf data in a custom print preview dialog.
void DisplayPdfDataInPrintDialog(Stream pdfData)
{
    // You can display the printable pdf data in a custom print preview dialog to the end user.
}

Remarks

Stream will be rewound to the start of the pdf data.

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

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

Applies to