How MHTML Merging Works in C#
Aspose.HTML for .NET
renders multiple MHTML or MHT archive streams through one MhtmlRenderer and writes their content to a shared output device. Pass the streams to MhtmlRenderer.Render() in the required sequence to create one PDF, XPS, DOCX, or TIFF result. This workflow combines rendered archive content rather than creating a new editable MHTML archive.
C# MHTML Merger and Interactive Example
Use the interactive example to prepare the archive order and generate a matching C# workflow. Add the MHTML or MHT files in the required sequence and choose PDF, XPS, DOCX, or TIFF. The code panel updates the filenames, renderer, output device, and extension when the selection changes. You can also run the configured merge online to inspect the result.
using System.IO;
using Aspose.Html.Rendering;
using Aspose.Html.Rendering.Pdf;
// Open the MHTML files as streams in the required merge order.
using FileStream stream1 = File.OpenRead("first.mhtml");
using FileStream stream2 = File.OpenRead("second.mhtml");
// Create the renderer and the PDF output device.
using MhtmlRenderer renderer = new MhtmlRenderer();
using PdfDevice device = new PdfDevice("merged.pdf");
// Render the sources into one PDF file.
renderer.Render(device, stream1, stream2);
MHTML Merger Workflow
PDF is selected by default. The renderer remains MhtmlRenderer; the output device changes according to the selected target format.
- Open every
.mhtmlor.mhtsource withFile.OpenRead()or provide another readableStreamimplementation. - Keep the streams in the order in which their rendered content must appear.
- Create a
MhtmlRendererinstance. - Create the output device, such as
PdfDevicefor PDF. - Pass the device and all archive streams to
MhtmlRenderer.Render().
Choose an MHTML Merger Output Format
- PDF – create a fixed-layout document for distribution or printing.
- XPS – render the web archives into an XPS document.
- DOCX – produce a document that can be opened in Microsoft Word or another compatible editor.
- TIFF – store the combined rendered pages as raster image output.
The generated C# selects PdfDevice, XpsDevice, DocDevice, or ImageDevice for the chosen target. See
Rendering Devices in C#
for page, image, and document settings.
MHTML Merging Notes
- Source order controls result order. The archives are rendered in the same sequence in which their streams are passed to
MhtmlRenderer.Render(). - Keep every input stream open. Do not dispose a stream until the renderer finishes writing the combined output.
- The result is not a new MHTML archive. The MIME parts are interpreted and rendered rather than consolidated into another editable web archive.
- Layout settings belong to the output device. Configure page size, margins, resolution, or other target-specific options through the matching rendering options class.
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.
MHTML Merger FAQ
1. Is there a difference between MHTML and MHT input files?
MHTML and MHT are commonly used extensions for the same MIME HTML web archive format. MhtmlRenderer processes their archive streams in the same workflow, provided the MIME structure and referenced parts are valid.
2. Can I limit the time allowed for MHTML rendering?
Yes. Use a MhtmlRenderer.Render() overload that accepts a TimeSpan or CancellationToken. A limit is useful when archives contain complex pages, scripts, or resources that could delay completion.
3. Are embedded images, styles, and fonts included in the result?
The renderer uses valid MIME parts referenced by the archived page. Missing parts, malformed content locations, or unavailable external resources can lead to incomplete styling, images, or fonts in the rendered output.
4. Can I pass HTML documents and MHTML streams to the same renderer call?
No. MhtmlRenderer expects MHTML streams, while HtmlRenderer expects HTMLDocument objects. Use the renderer that matches the source collection or normalize the inputs through a separate, explicitly designed workflow.
5. Can the merged PDF be written to a stream?
Yes. Create PdfDevice with a writable Stream, then pass the device and MHTML input streams to MhtmlRenderer.Render(). Keep the output stream open until rendering completes if the application needs to read or transfer the result afterward.
Related File Mergers
Choose another source format or return to the merger overview.