Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created April 22, 2022 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/1f36aa85dca7fa2c31ea770ac0afbdda to your computer and use it in GitHub Desktop.
Save aspose-com-gists/1f36aa85dca7fa2c31ea770ac0afbdda to your computer and use it in GitHub Desktop.
Convert HTML to MHT or MHTML File Programmatically in C# .NET
// Prepare HTML code with a link to another file and save it to the file as 'document.html'
string code = "<span>Hello, World!!</span> " +
"<a href='document2.html'>click</a>";
File.WriteAllText("document.html", code);
// Prepare HTML code and save it to the file as 'document2.html'
code = @"<span>Hello, World!!</span>";
File.WriteAllText("document2.html", code);
// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
MHTMLSaveOptions options = new MHTMLSaveOptions()
{
ResourceHandlingOptions =
{
MaxHandlingDepth = 1
}
};
// Convert HTML to MHTML
Converter.ConvertHTML("document.html", options, "output.mht");
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("input.html");
// Initialize MHTMLSaveOptions object
MHTMLSaveOptions options = new MHTMLSaveOptions();
// Convert HTML to MHTML
Converter.ConvertHTML(document, options, "output.mht");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment