Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert HTML to MHTML in C#, use
Converter.ConvertHTML() with an HTML source,
MHTMLSaveOptions, and an output path. Aspose.HTML for .NET can package HTML and related resources into a single MHTML web archive, and MHTMLSaveOptions lets you control page URL restrictions, resource URL restrictions, and handling depth.
This guide shows the core HTML to MHTML workflows for C# applications: a one-line conversion, a file-based conversion, and custom MHTML save options. Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.
MHTML combines HTML markup and external resources, such as images, CSS, scripts, and other linked assets, into one archive file. Use HTML to MHTML conversion when your application needs a portable single-file snapshot of a web page or generated HTML content.
For simple input, the static
Converter class provides a short path from HTML content to an MHTML file. The example below passes HTML code, a base URI, default MHTMLSaveOptions, and the output file path to ConvertHTML().
1// Convert HTML to MHTML using C#
2
3// Invoke the ConvertHTML() method to convert HTML to MHTML
4Converter.ConvertHTML(@"<h1>Hellow, Word!</h1>", ".", new MHTMLSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.mht"));Use this pattern when your HTML is already available as a string and default MHTML packaging settings are enough. For file input or custom resource handling, use the workflows below.
A typical file-based conversion uses an
HTMLDocument instance, an MHTMLSaveOptions object, and Converter.ConvertHTML(). You can load HTML from a local file, HTML code, stream, or URL. For more loading patterns, see
Creating an HTML Document.
The workflow is:
HTMLDocument constructor.MHTMLSaveOptions instance.Converter.ConvertHTML(document, options, savePath) to write the MHTML archive.The following C# code converts an HTML document to MHTML.
1// Convert HTML to MHTML in C#
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "drawing.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "drawing-output.mht");
8
9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize MHTMLSaveOptions
13MHTMLSaveOptions options = new MHTMLSaveOptions();
14
15// Convert HTML to MHTML
16Converter.ConvertHTML(document, options, savePath);MHTMLSaveOptions, and output path to Converter.ConvertHTML().HTMLDocument, create MHTMLSaveOptions, and call Converter.ConvertHTML(document, options, savePath).HTMLDocument from the URL, then convert the loaded document to a single-file archive.MHTMLSaveOptions.PageUrlRestriction and MaxHandlingDepth.MHTMLSaveOptions.ResourceUrlRestriction for CSS, images, scripts, and other linked files.Use
MHTMLSaveOptions when the output archive needs custom resource handling. The options object is passed to ConvertHTML() and controls which pages and resources are included in the generated MHTML file.
| Option | Use it to control |
|---|---|
PageUrlRestriction | Restrictions applied to URLs of handled pages. The default value is RootAndSubFolders. |
ResourceUrlRestriction | Restrictions applied to handled resources such as CSS, JavaScript, images, and other linked files. The default value is SameHost. |
MaxHandlingDepth | Maximum depth of pages that will be handled during packaging. The default value is 0. |
The following example creates an MHTML file with custom save options.
1// Convert HTML to MHTML with custom settings using C#
2
3// Prepare HTML code with a link to another file and save it to the file as 'document.html'
4string code = "<span>Hello, World!!</span> " +
5 "<a href='document2.html'>click</a>";
6File.WriteAllText("document.html", code);
7
8// Prepare HTML code and save it to the file as 'document2.html'
9code = @"<span>Hello, World!!</span>";
10File.WriteAllText("document2.html", code);
11
12string savePath = Path.Combine(OutputDir, "output-options.mht");
13
14// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
15MHTMLSaveOptions options = new MHTMLSaveOptions()
16{
17 ResourceHandlingOptions =
18 {
19 MaxHandlingDepth = 1
20 }
21};
22
23// Convert HTML to MHTML
24Converter.ConvertHTML("document.html", options, savePath);In this example, MHTMLSaveOptions is passed to Converter.ConvertHTML() together with the loaded HTML document and output path. The sample uses MaxHandlingDepth = 1, which means only pages directly referenced from the saved document will be handled.
For deeper resource and rendering settings, see Fine-Tuning Converters.
PdfSaveOptions.DocSaveOptions.Use the online HTML to MHTML Converter for quick manual conversion. Use Aspose.HTML for .NET when HTML to MHTML packaging must run inside a C# application, service, or archive workflow.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.