Create an MHTML Archive from Edited HTML
Aspose.HTML for .NET
loads HTML into an editable
HTMLDocument
. After a C# application changes DOM nodes, text, attributes, or styles,
MHTMLSaveOptions
packages the resulting HTML and handled resources as an MHTML or MHT web archive.
This workflow creates a new MHTML archive from editable HTML. It does not open an existing .mhtml or .mht archive as an editable HTML DOM.
Update HTML Content and Save It as MHTML
The following example loads input.html, updates the first paragraph, and writes the modified document and its handled resources to edited.mhtml.
C# Code to Create MHTML from Edited HTML
using Aspose.Html;
using Aspose.Html.Dom;
using Aspose.Html.Saving;
using HTMLDocument document = new HTMLDocument("input.html");
// Update the HTML DOM before creating the archive
Element paragraph = document.QuerySelector("p");
if (paragraph != null)
{
paragraph.TextContent = "Updated paragraph text.";
paragraph.SetAttribute("class", "updated-content");
}
// Package the edited HTML and handled resources as MHTML
MHTMLSaveOptions options = new MHTMLSaveOptions();
document.Save("edited.mhtml", options);
Steps to Create MHTML from Edited HTML
- Load the editable source with an
HTMLDocumentconstructor. - Locate the required DOM node with
QuerySelector()or another navigation method. - Update the element content, attributes, or styles before archive creation.
- Create an
MHTMLSaveOptionsinstance and configure resource handling when the default behavior is not sufficient. - Call
Save()with the MHTML options and the.mhtmlor.mhtoutput path.
MHTML Creation Capabilities and Limitations
- Edit before packaging. Change the HTML DOM with the same node, selector, attribute, text, and style APIs used by the HTML editing workflow .
- Create a single-file web archive. Package the edited HTML and handled resources into an MHTML file for storage, transfer, or offline use.
- Control resource handling. Use
MHTMLSaveOptions.ResourceHandlingOptionsto configure URL restrictions, linked-page depth, and resource processing for the archive workflow. - Account for inaccessible resources. A remote image, stylesheet, font, or script can be omitted when it cannot be retrieved or is excluded by the configured restrictions.
- Process existing archives through dedicated workflows. Use MHTML to PDF for fixed-layout output or MHTML Merger to render several archives into one result.
- Use the conversion page for packaging-only tasks. When no DOM changes are required, see HTML to MHTML for focused conversion examples.
HTML Editing and MHTML Documentation
Read
Edit HTML Document
for DOM modification examples. The
Convert HTML to MHTML
guide explains archive creation and MHTMLSaveOptions. For the distinction between editable HTML documents and MHTML rendering workflows, see
Create or Load HTML Documents
.
Get Started with Aspose.HTML for .NET API
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
HTML to MHTML Workflow FAQ
1. Can I create MHTML from an HTML string or stream?
Yes. Create an HTMLDocument from the string or stream and provide a base URL when the content uses relative resource references. Apply the required DOM changes, then call Save() with MHTMLSaveOptions to write the archive.
2. Are MHTML and MHT different formats?
No. .mhtml and .mht are commonly used extensions for the same MIME HTML web archive format. Choose the extension required by the receiving application or storage convention.
3. Will the MHTML output contain every linked image, stylesheet, font, and script?
The archive can include handled resources that are accessible and permitted by the resource settings. Invalid URLs, authentication requirements, network restrictions, or save-option filters can prevent a resource from being included.
4. Does saving MHTML change the source HTML file?
No. The DOM changes exist in the in-memory HTMLDocument, and the MHTML save operation writes a separate archive to the selected output path. The source file changes only if the application explicitly saves over it.
5. Is MHTML output equivalent to PDF output?
No. MHTML is a MIME web archive that stores HTML and related resources for later browser-style processing. PDF is a fixed-layout rendered document intended for consistent viewing, printing, and distribution.
Related HTML and MHTML Workflows
Continue with editing, archive creation, conversion, or merging.