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 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.
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.