The SaveDocument method saves the current page to a
local file in the format specified by the uFileType parameter.
The operation is synchronous, processing messages while the browser
completes the action, and only returns once the document has been
created or the operation has failed. A temporary file is created
during the save process and then moved into place, replacing any
existing file. This ensures that an existing file will not be changed
unless the browser indicates the save operation was successful. Saving
is not permitted while a navigation is in progress, and attempts to do
so will fail with an error.
The file name specified must be valid, and environment variables in
the path are expanded before use. When WEBVIEW_FILE_TYPE_DEFAULT
is specified, the format is inferred from the file extension. For
example, .html or .htm will save the document
as HTML, .pdf will create a PDF file and .png
will save the page as a PNG image. If no recognized extension is present,
the page will be saved as plain text by default.
When saving the current document as plain text, the HTML markup
will be removed and it will be saved as a UTF-8 encoded text file. When
saving as HTML, the method saves the complete HTML source for the page.
The JSON format also saves the HTML markup, but encodes it as a quoted
JSON string with reserved characters escaped. Note that all
text-related formats are saved using UTF-8 multi-byte encoding without
a byte order mark (BOM). The text will not be automatically converted
to the current user's locale. Most modern text editors automatically
recognize UTF-8 encoding, but some legacy applications may assume the
text is localized to the current code page.
The MHTML option creates a single-file archive which contains the
page content and all of its resources such as images, style sheets,
and scripts. This format is often used for offline archiving or
sharing since everything is contained in one file. Saving as PDF
renders the page into a paginated print-style format suitable for
distribution. These formats all operate on the complete document,
including content that extends beyond the visible area.
By contrast, saving as an image (JPEG, PNG, or WebP) captures only
the visible viewport of the page at the time the method is called.
Content that requires scrolling will not be included in the saved
image. PNG is lossless and preserves transparency, while JPEG and WebP
use compression to produce smaller files. WEBP generally offers better
quality at a reduced size compared to JPEG, but support for the format
may vary between applications. If you want a pixel-perfect
representation of the current page as it appears on the screen,
particularly if it contains a lot of text, it is
recommended that you use the PNG format.
Because this method blocks the current thread until the save
completes, it may take a noticeable amount of time to return if the
current document is very large. If the method is called from a worker
thread, the request is marshaled to the UI thread that manages the
browser control. For applications with a graphical interface, it is
recommended that you display the standard hourglass (or other busy
indicator) while the save is in progress to provide a visual cue to
the user.
When saving a document as a PDF without providing any print
settings, this method uses the default print settings provided by the
Microsoft Edge WebView2 runtime. These defaults include portrait
orientation, US Letter paper size (8.5 × 11 inches), a scale factor of
100%, and margins of approximately 0.4 inches on all sides. Background
colors and images are not printed, and headers and footers are enabled
by default. The resulting PDF includes all pages of the document.
When saving the document as a PDF with print settings, the
WEBVIEW_PRINT_SETTINGS structure can be used to
control page dimensions, margins, scaling, orientation, background
colors and images, and headers and footers. The flags in the structure's
dwFlags member determine which optional settings are
applied:
WEBVIEW_PRINT_FLAG_PAGE_SIZE applies
dPageWidth and dPageHeight, in inches. Each
invalid dimension is replaced with the corresponding
library default.
WEBVIEW_PRINT_FLAG_MARGINS applies all
four margin members, in inches. Zero requests no margin;
invalid values are converted to zero.
WEBVIEW_PRINT_FLAG_SCALE applies dScale.
A value of 1.0 represents 100 percent. Non-zero
values are limited to the range 0.1 through 2.0. Zero or
an invalid value leaves the default scale unchanged.
WEBVIEW_PRINT_FLAG_BACKGROUND includes
document background colors and images. Backgrounds are
omitted when this flag is not specified.
WEBVIEW_PRINT_FLAG_HEADERS includes both
a header and a footer. The lpszHeaderText and
lpszFooterText members can specify custom text.
Headers and footers are omitted when this flag is not
specified.
The nOrientation member selects portrait or landscape
orientation independently of dwFlags. Setting page
dimensions, margins, or scaling without the corresponding flag
has no effect. In particular, zero-initialized margins specify
no margins when WEBVIEW_PRINT_FLAG_MARGINS is enabled;
they do not request the default margins.
When headers and footers are enabled, a NULL
lpszHeaderText uses the document title, and a NULL
lpszFooterText uses the document URI. An empty string
suppresses the corresponding text without disabling the other
header or footer elements. Allow suitable top and bottom margins
for this content.
Printer-specific structure members are ignored when saving
directly to PDF. These members are nColorMode, nCopies,
nDuplex, nPagesPerSide, nCollation,
lpszPrinter, and lpszRanges. Consequently, these
settings cannot request grayscale PDF output, restrict the PDF
to a page range, or place multiple document pages on each PDF
page.
PDF output renders the complete document using the browser's
print layout, including content beyond the visible browser
area. The document's print styles can affect its appearance and
pagination. PDF output can therefore differ from the page as
displayed on the screen.
If you want a copy of the HTML or plain text contents of the current
page without saving it to a file, use the GetDocumentText
or GetFilteredText methods.
If you wish to save the document as a PDF to memory and don't want
the intermediate step of saving it to disk, the SaveDocumentToMemory
method can be used to copy the document to a buffer that your
application provides. This requires Microsoft Edge WebView2 Runtime
version 109.0.1518.0 or later. Earlier versions of the runtime do not
support the PrintToPdfStream method used to generate
PDF documents in memory. If the installed runtime does not support
this functionality, the method fails with the error code
ST_ERROR_OPERATION_NOT_SUPPORTED.