The SaveDocument method saves the current page to a
local file in the format specified by the FileType 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 no file type 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 a document in PDF format, the control uses the configured
page layout and printing properties. These include the page dimensions,
margins, orientation, scaling, headers and footers, and background
printing. Applications can modify these properties before calling the
SaveDocument method to customize the appearance of the generated
PDF document. If a property has not been explicitly set, the default
value provided by the WebView2 runtime is used.
PDF documents are generated directly by the WebView2 runtime without
displaying a print dialog or requiring a printer to be installed.
Printer-specific properties, such as PrinterName and
PrintColorMode, do not affect PDF generation. The
PrintDialog property also has no effect when saving a document
in PDF format.
The page layout and printing properties are only used when saving
documents in PDF format. These settings have no effect when saving
documents as plain text, HTML, JSON, or MHTML, or when capturing the
current browser viewport as a JPEG, PNG, or WebP image. In particular,
page dimensions, margins, orientation, and scaling do not affect the
size or appearance of saved images, which are determined by the
dimensions and content of the browser viewport.
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 uses lossless compression, while JPEG uses lossy
compression. WebP supports both lossy and lossless compression and can
provide smaller files while maintaining good image quality. 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.
If you want a copy of the HTML or plain text contents of the current
page without saving it to a file, use the DocumentText
or FilteredText properties.