SaveDocument Method  
 

Saves the currently displayed document in a specified format such as HTML, PDF, MHTML, or in an image format.

Syntax

object.SaveDocument( FileName, [FileType] )

Parameters

FileName
A string which specifies the name of the local file. If the path includes environment variables enclosed in percent (%) symbols, the variables will be expanded. This parameter must specify a valid file path. The file name cannot specify a device, folder name or system file. If the file is marked as read-only, the method will fail.
FileType
An optional parameter which specifies the file format. If this parameter is omitted, the format will be based on the file name extension.
Value Value Description
webSaveAsDefault 0 Save using a format inferred from the file extension. If the file extension is not recognized, the contents will be saved as plain text. For example, if you specify a file name with the .html extension, it will be saved in HTML format. If the file name has a .pdf extension, it will be saved as a PDF.
webSaveAsText 1 Saves the current document as plain text. The HTML tags are removed and entities are converted to their UTF-8 equivalent. Extraneous whitespace within the text will be removed, but the overall structure of ordered and unordered lists will be preserved. Choose this format if you wish to save only the text content of a document.
webSaveAsHtml 2 Saves the current document as a UTF-8 encoded HTML file. This includes the complete source for the page, but does not include any of the additional assets such as images. Choose this format if you wish to save the document as an editable HTML file.
webSaveAsJson 3 Saves the current document as a JSON formatted string. The string will be quoted and reserved characters will be escaped. Choose this format if you wish to preserve the HTML document as a JSON string for logging or embedding in a JSON object.
webSaveAsMhtml 4 Saves the current document as a single-file web archive (MHTML) that inlines the HTML content and all of the resources it references. This format calls the DevTools Page.captureSnapshot method in the browser and writes the returned data to disk. MHTML is useful for offline archiving since it combines images, styles and scripts together in a single file.
webSaveAsPdf 5 Saves the current document as a PDF file, producing a paginated document suitable for printing and distribution. The document is generated directly by the WebView2 runtime without displaying a print dialog. The page layout and appearance can be customized using the control's printing properties.
webSaveAsJpeg 101  Save the current document view as a JPEG image. JPEG is a widely used image format optimized for photographs and complex images with smooth gradients. It uses lossy compression to significantly reduce file size while maintaining acceptable visual quality.
webSaveAsPng 102 Save the current document view as a PNG image. PNG is a lossless image format designed for crisp detail and high fidelity, especially in graphics with sharp edges, text, or flat colors. Although PNG files are typically larger than JPEGs, they preserve exact image data without quality loss.
webSaveAsWebP 103 Save the current document view as a WebP image. WebP is a modern image format developed by Google that supports both lossy and lossless compression, as well as transparency and animation. It delivers smaller file sizes compared to JPEG and PNG while maintaining comparable or better visual quality.

Return Value

Returns True if the current document was saved to the specified file, otherwise the method returns False.

Remarks

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.

Example

This example saves the current document as a PDF using 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. The PDF document is generated directly without displaying a print or file dialog. The configured page layout settings are applied when the document is saved.

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

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

' Scale the document to 90%
WebView1.PrintScale = 0.9

' Include background colors and images
WebView1.PrintBackground = True

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

' Save the document as a PDF
If Not WebView1.SaveDocument("C:\Reports\Monthly.pdf", webSaveAsPdf) Then
    MsgBox "Unable to save the PDF document."
End If

See Also