Convert MHTML to PNG in Java

To convert MHTML to PNG in Java, open the MHTML source as a file stream, create ImageSaveOptions with ImageFormat.Png, and call Converter.convertMHTML(). PNG is also the default output format of the parameterless ImageSaveOptions() constructor.

Converting MHTML to PNG produces a static lossless image of archived web content. PNG is a suitable choice for text, diagrams, interface captures, and other output where sharp edges or transparency are more important than the smaller files typically produced by JPG compression.

Convert an MHTML File to PNG

Follow these steps for a basic MHTML-to-PNG conversion:

  1. Open the source MHTML file as a FileInputStream.
  2. Create ImageSaveOptions with ImageFormat.Png.
  3. Call Converter.convertMHTML() with the stream, options, and output path.

The following example converts sample.mht to sample-output.png.

 1// Convert MHTML to PNG using Java
 2
 3// Open an existing MHTML file for reading
 4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
 5
 6// Initialize ImageSaveOptions
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
 8
 9// Call the convertMHTML() method to convert MHTML to PNG
10Converter.convertMHTML(fileInputStream, options, "sample-output.png");

Download complete Java examples and data files from GitHub.

Customize PNG Page Size and Background

ImageSaveOptions provides access to image format, page setup, resolution, background color, CSS processing, text rendering, and other output settings. Its default page background is transparent, although backgrounds defined by the archived HTML and CSS can still be rendered.

To convert MHTML to PNG with custom page settings:

  1. Open the source MHTML file as a stream.
  2. Create ImageSaveOptions with ImageFormat.Png.
  3. Create and configure a PageSetup object with the required page dimensions.
  4. Assign the page setup to the save options and set the required background color.
  5. Pass the stream, options, and output path to Converter.convertMHTML().

The following example converts sample.mht to sample-options.png, sets a green background, and uses a 3000 × 1000 pixel page.

 1// Convert MHTML to PNG in Java with custom settings
 2
 3// Open an existing MHTML file for reading
 4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
 5
 6// Initialize the ImageSaveOptions with a custom page-size and background-color
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
 8PageSetup pageSetup = new PageSetup();
 9Page anyPage = new Page();
10anyPage.setSize(
11        new Size(
12                Length.fromPixels(3000),
13                Length.fromPixels(1000)
14        )
15);
16pageSetup.setAnyPage(anyPage);
17options.setPageSetup(pageSetup);
18options.setBackgroundColor(Color.getGreen());
19
20// Call the convertMHTML() method to convert MHTML to PNG
21Converter.convertMHTML(fileInputStream, options, "sample-options.png");

For additional image settings, see Fine-Tuning Converters.

Related MHTML Conversion Guides

Other Platforms

FAQ

Can I convert MHTML to PNG for free?

Yes. Use the free online MHTML to PNG Converter for a quick manual conversion. Aspose.HTML for Java is a commercial API for programmatic conversion and can be evaluated before licensing; see Licensing.

Can MHTML-to-PNG conversion preserve transparency?

PNG supports transparency, and the default page fill in ImageSaveOptions is transparent. However, a background color defined in the archived page or its CSS is still part of the rendered content. Call setBackgroundColor() when you need a specific page background.

Try Online MHTML to PNG Conversion

Use the free online MHTML to PNG Converter for quick manual conversion without writing Java code.