PrintDocument Method  
 

Prints the currently loaded document, optionally displaying the standard browser print dialog.

Syntax

object.PrintDocument

Parameters

None.

Return Value

Returns True if the print request was successfully initiated, or False if the method fails.

Remarks

The PrintDocument method prints the currently loaded document using the Microsoft Edge WebView2 printing engine. By default, the standard browser print dialog is displayed, allowing the user to select a printer, configure printing options, and confirm printing. The dialog is displayed asynchronously, and the method returns after the print operation has been initiated.

The PrintDialog property determines whether the print dialog is displayed. When this property is False, the document is printed directly without displaying a dialog or requiring user interaction. The control uses the printer specified by the PrinterName property, or the default Windows printer if no printer name has been specified. Printer-specific settings, such as the color printing mode, are applied when printing directly.

The control provides several properties that can be used to customize the appearance of printed documents, including page dimensions, margins, orientation, scaling, headers and footers, and background printing. These properties can be configured before calling the PrintDocument method. Many of these settings also apply when saving a document in PDF format using the SaveDocument method.

This method can only be called after navigation has completed and the current document is fully loaded. If navigation is still in progress when the method is called, it will fail with an error indicating that the operation is in progress.

Printing is performed asynchronously by the WebView2 runtime. A successful return value indicates that the print request was accepted, but does not guarantee that the document has been printed. The user may cancel printing from the print dialog, or a printer may be unavailable or encounter an error after the request has been submitted. Applications that require confirmation that a document has been printed must monitor the printer or its print queue separately.

Example

This example configures the document to use US Letter paper with half-inch margins and portrait orientation. The document is scaled to 90% of its normal size, background printing is enabled, and custom header and footer text is specified. Setting the PrintDialog property to False allows the document to be printed directly to the default Windows printer without displaying a print dialog. To print to a specific printer, assign its name to the PrinterName property before calling the PrintDocument method.

' Configure the page layout
WebView1.PageWidth = 8.5
WebView1.PageHeight = 11.0
WebView1.PrintOrientation = webPrintOrientationPortrait

' Set half-inch margins on all sides
WebView1.MarginTop = 0.5
WebView1.MarginBottom = 0.5
WebView1.MarginLeft = 0.5
WebView1.MarginRight = 0.5

' Print at 90% of the normal size
WebView1.PrintScale = 0.9

' Include background colors and images
WebView1.PrintBackground = True

' Enable headers and footers with custom text
WebView1.PrintHeaders = True
WebView1.HeaderText = "Monthly Report"
WebView1.FooterText = "Confidential"

' Use the printer's default color mode
WebView1.PrintColorMode = webPrintModeDefault

' Print directly to the default Windows printer
WebView1.PrinterName = ""
WebView1.PrintDialog = False

' Submit the document for printing
If Not WebView1.PrintDocument() Then
    MsgBox "Unable to initiate printing."
End If    

See Also