Convert HTML to MHTML in C#
MHTML combines normal HTML with external resources like images, animations, audio, etc., into one file with the .mht file extension. MHTML uses the MIME email protocol to combine items into a single web page archive file. With MHTML, the archival of online web pages becomes much easier and less cluttered.
In this article, you will find information on how to convert HTML to MHTML and how to use MHTMLSaveOptions.
Online HTML Converter
You can convert HTML to MHTML with Aspose.HTML for .NET API in real time. First, load an HTML file from your local drive and then run the example. In this example, the save options are set by default. You will immediately receive the conversion result as a separate file.
If you want to convert HTML to MHTML programmatically, please see the following C# code examples.
HTML to MHTML by a single line of code
The static methods of the Converter class are primarily used as the easiest way to convert an HTML code into various formats. You can convert HTML to MHTML in your C# application literally with a single line of code!
1// Invoke the ConvertHTML() method to convert HTML to MHTML
2Converter.ConvertHTML(@"<h1>Hellow, Word!</h1>", ".", new MHTMLSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.mht"));
Convert HTML to MHTML
Converting a file to another format using the ConvertHTML() method is a sequence of operations among which document loading and saving. The next example explains how to convert HTML to MHTML by line by line:
- Load the HTML file using HTMLDocument class.
- Create an instance of the MHTMLSaveOptions class.
- Use the ConvertHTML() method of Converter class to save HTML document as an MHTML file. You need to pass the HTMLDocument, MHTMLSaveOptions, and output file path to the ConvertHTML() method to convert HTML to MHTML.
1// Prepare a path to a source HTML file
2string documentPath = Path.Combine(DataDir, "drawing.html");
3
4// Prepare a path to save the converted file
5string savePath = Path.Combine(OutputDir, "drawing-output.mht");
6
7// Initialize an HTML document from the file
8using var document = new HTMLDocument(documentPath);
9
10// Initialize MHTMLSaveOptions
11var options = new MHTMLSaveOptions();
12
13// Convert HTML to MHTML
14Converter.ConvertHTML(document, options, savePath);
Save Options
Aspose.HTML allows converting HTML to MHTML using default or custom save options. MHTMLSaveOptions usage enables you to customize the rendering process; you can specify the PageUrlRestriction, ResourceUrlRestriction, MaxHandlingDepth, etc.
Property | Description |
---|---|
PageUrlRestriction | This property gets or sets restrictions applied to URLs of handled pages. The default value is RootAndSubFolders. |
ResourceUrlRestriction | Gets or sets restrictions applied to URLs of handled resources such as CSS, js, images, etc. The default is SameHost. |
MaxHandlingDepth | Gets or sets the maximum depth of pages that will be handled. |
To learn more about MHTMLSaveOptions, please read the Fine-Tuning Converters article.
Convert HTML to MHTML using MHTMLSaveOptions
To convert HTML to MHTML with MHTMLSaveOptions specifying, you should follow a few steps:
- Load or prepare an HTML file.
- Create a new MHTMLSaveOptions object.
- Use the ConvertHTML() method of the Converter class to save HTML as a MHTML file. You need to pass the HTMLDocument, MHTMLSaveOptions, and output file path to the ConvertHTML() method to convert HTML to MHTML.
The following example shows how to use MHTMLSaveOptions and create an MHTML file with custom save options:
1// Prepare HTML code with a link to another file and save it to the file as 'document.html'
2var code = "<span>Hello, World!!</span> " +
3 "<a href='document2.html'>click</a>";
4File.WriteAllText("document.html", code);
5
6// Prepare HTML code and save it to the file as 'document2.html'
7code = @"<span>Hello, World!!</span>";
8File.WriteAllText("document2.html", code);
9
10string savePath = Path.Combine(OutputDir, "output-options.mht");
11
12// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
13var options = new MHTMLSaveOptions()
14{
15 ResourceHandlingOptions =
16 {
17 MaxHandlingDepth = 1
18 }
19};
20
21// Convert HTML to MHTML
22Converter.ConvertHTML("document.html", options, savePath);
The
MHTMLSaveOptions() constructor initializes an instance of the MHTMLSaveOptions class that is passed to ConvertHTML() method. The ConvertHTML() method takes the document
, options
, output file path savePath
and performs the conversion operation. The MHTMLSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting HTML to MHTML format.
In the above example, we use the property MaxHandlingDepth = 1
means that only pages directly referenced from the saved document will be handled.
You can download the complete examples and data files from GitHub.
Aspose.HTML offers a free online HTML to MHTML Converter that converts HTML to MHTML with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!