CWebView::PrintDocument Method  
 
BOOL PrintDocument(
  DWORD dwOptions  
);
BOOL PrintDocument(
  DWORD dwOptions,  
  LPWEBVIEW_PRINT_SETTINGS lpSettings  
);

The PrintDocument method displays the browser print dialog for the current document.

Parameters

dwOptions
An unsigned integer which specifies how the print dialog should be displayed.
Value Description
WEBVIEW_PRINT_DIALOG_DEFAULT Use Edge's print preview dialog, equivalent to the Ctrl+P experience. This dialog provides preview capabilities and additional print options. This option is only supported when thelpSettings parameter is omitted or NULL. A print preview dialog will not be shown if printer settings are specified.
WEBVIEW_PRINT_DIALOG_SYSTEM Use the native system print dialog. This dialog allows the user to select a printer and set standard options, but does not provide a preview of the document. This option is only supported when the lpSettings parameter is omitted or NULL. A print preview dialog will not be shown if printer settings are specified.
WEBVIEW_PRINT_DIALOG_NONE Do not display a print preview dialog. If the lpSettings parameter is NULL, dialog behavior is determined by the value of the dwOptions parameter.
lpSettings
A pointer to a WEBVIEW_PRINT_SETTINGS structure which specifies one or more settings which control how the current document is printed. If this parameter is NULL, the document will be sent to the operating system's default printer, using the default print settings for the browser. The dwSize member of this structure must be initialized to the size of the structure or the function will fail. Structure members which are zero or NULL are interpreted according to their individual defaults and the flags specified with the dwFlags member.

Return Value

If the method succeeds, the return value is non-zero. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The PrintDocument method displays the print dialog for the currently loaded document. The dialog is shown asynchronously and the method will return once the print user interface has been invoked. This method does not perform the printing operation itself; it only initiates the print workflow and allows the user to select a printer, adjust preferences, and confirm printing.

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. This method only supports interactive printing through a user interface. If you need to programmatically export the document without user interaction, use the SaveDocument method instead.

It is recommended that most applications use the default dialog. The WEBVIEW_PRINT_DIALOG_SYSTEM option shows a simplified print dialog provided by browser. This is not the same dialog exposed by the Windows Common Controls API through the PrintDlg or PrintDlgEx functions, and it may differ in appearance and available options depending on the platform and version of the WebView2 runtime.

To provide more control over how the document is printed, initialize the entire WEBVIEW_PRINT_SETTINGS structure to zero, then initialize its dwSize member to the size of the structure. Set the remaining members to the options required for the print operation. If the lpszPrinter member is NULL or empty, the operating system's default printer is used.

The dwFlags structure member controls whether custom page dimensions, margins, and scaling are applied, and whether backgrounds and headers and footers are included. For example, setting margin values has no effect unless WEBVIEW_PRINT_FLAG_MARGINS is also specified. When this flag is specified, all four margin members are used, including any members set to zero.

Page dimensions and margins are specified in inches. Scaling is specified as a factor, where 1.0 represents 100 percent. Printer settings such as color mode, duplex printing, and custom media sizes are subject to the capabilities of the selected printer.

To save the document directly to a PDF file, use SaveDocument method.

Example

WEBVIEW_PRINT_SETTINGS settings = { 0 };

settings.dwSize = sizeof(settings);
settings.dwFlags = WEBVIEW_PRINT_FLAG_MARGINS |
                   WEBVIEW_PRINT_FLAG_BACKGROUND;

settings.dMarginLeft = 0.5;
settings.dMarginTop = 0.5;
settings.dMarginRight = 0.5;
settings.dMarginBottom = 0.5;

settings.nOrientation = WEBVIEW_PRINT_ORIENTATION_PORTRAIT;
settings.nCopies = 1;
settings.nDuplex = WEBVIEW_PRINT_DUPLEX_SIMPLEX;

if (!pWebView->PrintDocument(WEBVIEW_PRINT_DIALOG_NONE, &settings))
{
    DWORD dwError = pWebView->GetLastError();

    // Report or otherwise handle dwError here
    return FALSE;
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib

See Also