Convert MHTML to PNG in Java

Aspose.HTML for Java library provides a wide range of MHTML conversions to images, such as JPG, PNG, BMP, TIFF, and GIF. Converting MHTML to any supported image format follows the mandatory steps:

To specify the output image format, use the ImageSaveOptions class. The ImageSaveOptions(format) constructor initializes the options object with the format specifying. You can set the image format to JPG, PNG, BMP, GIF, or TIFF. The default ImageFormat is PNG.

In this article, you will find information on how to convert an MHTML to PNG using Aspose.HTML for Java and how to apply ImageSaveOptions and MemoryStreamProvider. You can easily use Java examples to convert MHTML to PNG, which are detailed here for converting MHTML to JPG, MHTML to BMP, MHTML to GIF, and MHTML to TIFF. Just set the ImageFormat to ImageSaveOptions!

Convert MHTML to PNG

Let’s walk through the step-by-step instructions for a simple MHTML to PNG conversion scenario:

  1. Open an MHTML file. In the example, we use the FileInputStream class to read an MHTML file as a stream of bytes.
  2. Create an instance of ImageSaveOptions. ImageFormat.Png will be used as default image format.
  3. Use the convertMHTML(stream, options, savePath) method of the Converter class to save MHTML as PNG image. The method takes as parameters stream, options, and savePath and performs the conversion.

Using convertMHTML() methods is the most common way to convert MHTML files into various formats. The following Java code snippet shows how to convert MHTML to PNG:

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

You can download the complete examples and data files from GitHub.

Save Options – ImageSaveOptions Class

Aspose.HTML for Java provides the ability to programmatically convert MHTML to PNG with full control over various conversion options using ImageSaveOptions. This feature allows users to customize the rendering process by setting the image format, page size, margins, compression level, media type, and other parameters.

MethodDescription
setCompression(value)Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW.
getCssGets a CssOptions object which is used for configuration of CSS properties processing.
setFormat(value)Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png.
setBackgroundColor(value)Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()).
setPageSetup(value)Gets a page setup object is used for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
setSmoothingMode(value)Sets the rendering quality for this image.
getText()Gets a TextOptions object which is used for configuration of text rendering.

For further information on how to customize the conversion process with ImageSaveOptions you can refer to the Fine-Tuning Converters article.

Convert MHTML to PNG using ImageSaveOptions

Aspose.HTML for Java allows converting MHTML to PNG using default or custom save options. Setting different options of ImageSaveOptions class gives you control over the page size, margins, background color, and resolution settings for the output image. These are the steps to convert an MHTML file to PNG format with ImageSaveOptions specifying:

  1. Open an MHTML file. Initialize a FileInputStream to read the contents of the MHTML file as a stream of bytes.
  2. Create a new ImageSaveOptions and specify the required save options:
    • Use the getBackgroundColor() method to set the background color of the image.
    • Use the setAnyPage() method to set the size of the image.
  3. Use the convertMHTML(stream, options, savePath) method to save MHTML file as a PNG image. The method takes the stream, options, output file path savePath and performs the conversion operation.

The following Java example shows how to use ImageSaveOptions and create a PNG file with custom page-size and background color:

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

Aspose.HTML offers a free online MHTML to PNG Converter that converts MHTML to PNG with high quality, easy and fast. Just upload, convert your files and get the result in a few seconds!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.