Convert MHTML to JPG in Java

To convert MHTML to JPG in Java, open the MHTML source as a file stream, create ImageSaveOptions with ImageFormat.Jpeg, and call Converter.convertMHTML(). Use ImageSaveOptions to configure page size, background color, resolution, and rendering options.

Converting MHTML to JPG produces a static raster representation of an archived web page. JPG uses lossy compression and is useful for previews, reports, presentations, and sharing when a smaller output file is more important than transparency or lossless detail.

Convert MHTML to JPG with Two Lines of Java Code

The shortest example opens an MHTML file and passes its stream, JPEG save options, and the output path directly to Converter.convertMHTML().

1// Convert MHTML to JPG using Java
2
3// Open an existing MHTML file for reading
4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
5
6// Invoke the convertMHTML() method to convert MHTML to JPG
7Converter.convertMHTML(fileInputStream, new ImageSaveOptions(ImageFormat.Jpeg), "convert-by-few-lines.jpg");

The sample converts sample.mht and saves the result as convert-by-few-lines.jpg.

Convert an MHTML File to JPG

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

  1. Open the source MHTML file as a FileInputStream.
  2. Create ImageSaveOptions with ImageFormat.Jpeg.
  3. Call Converter.convertMHTML(stream, options, outputPath) to convert MHTML to JPG.

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

 1// Convert MHTML to JPG in Java
 2
 3// Open an existing MHTML file for reading
 4java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
 5
 6// Create an instance of the ImageSaveOptions class
 7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
 8
 9// Call the convertMHTML() method to convert MHTML to JPG
10Converter.convertMHTML(fileInputStream, options, "sample-output.jpg");

Download complete Java examples and data files from GitHub.

Customize JPG Output with ImageSaveOptions

The ImageSaveOptions class controls the image format and rendering settings used during MHTML-to-image conversion.

MethodUse it to
setFormat(value)Select JPG, PNG, BMP, GIF, or TIFF. The default format is PNG.
setBackgroundColor(value)Set the color that fills each output page. The default is transparent.
getPageSetup()Access the page setup used to configure output page size and margins.
setHorizontalResolution(value)Set horizontal resolution in pixels per inch. The default is 300 dpi.
setVerticalResolution(value)Set vertical resolution in pixels per inch. The default is 300 dpi.
setUseAntialiasing(value)Enable or disable antialiasing. Antialiasing is enabled by default.
setCompression(value)Select TIFF compression. The default TIFF compression is LZW.
getCss()Access CSS processing options.
getText()Access text rendering options.

To convert MHTML to JPG with custom page settings:

  1. Open the source MHTML file as a stream.
  2. Create ImageSaveOptions with ImageFormat.Jpeg.
  3. Set the required background color and configure the page dimensions through PageSetup.
  4. Pass the stream, options, and output path to Converter.convertMHTML().

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

 1// Convert MHTML to JPG 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.Jpeg);
 8options.setBackgroundColor(Color.getGreen());
 9options.getPageSetup().setAnyPage(new Page());
10options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(1000), Length.fromPixels(500)));
11
12// Call the convertMHTML() method to convert MHTML to JPG
13Converter.convertMHTML(fileInputStream, options, "sample-options.jpg");

For additional image settings, see Fine-Tuning Converters.

Related MHTML Conversion Guides

Other Platforms

FAQ

Can I convert MHTML to JPG for free?

Yes. Use the free online MHTML to JPG 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.

Does a JPG file preserve links and other interactive content?

No. MHTML-to-JPG conversion produces static raster image data. Links, form controls, scripts, audio, video, and other interactive behavior are not preserved as interactive features in the JPG output.

Try Online MHTML to JPG Conversion

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

Free Online MHTML to JPG Converter