MHTML to JPG Conversion with the Java API

Aspose.HTML for Java converts an MHTML web archive to JPG with the Converter.convertMHTML() method. Open the .mht or .mhtml source as a stream, select JPEG output with ImageSaveOptions, and provide the destination path. The rendering pipeline processes the archived HTML and its stored resources to create a static image.


MHTML to JPG Java Code Example

The example opens an MHTML file, selects JPEG output, and saves the rendered result as output.jpg. A try-with-resources statement closes the input stream automatically.

Convert MHTML to JPG in Java

import com.aspose.html.converters.Converter;
import com.aspose.html.rendering.image.ImageFormat;
import com.aspose.html.saving.ImageSaveOptions;

import java.io.FileInputStream;

// Open the MHTML archive
try (FileInputStream inputStream = new FileInputStream("input.mhtml")) {
    // Select JPEG output
    ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);

    // Convert MHTML to JPG
    Converter.convertMHTML(inputStream, options, "output.jpg");
}

Test MHTML to JPG Conversion Online

Use the interactive converter to upload an MHTML file or provide its URL and inspect the JPG result before implementing the same operation in a Java application.

MHTML to JPG API Steps

  1. Add Aspose.HTML for Java to the project with Maven or Gradle.
  2. Open the source .mht or .mhtml archive as a FileInputStream.
  3. Create ImageSaveOptions with ImageFormat.Jpeg.
  4. Pass the stream, options, and output path to Converter.convertMHTML() .
  5. Close the input stream after conversion. A try-with-resources statement handles this automatically.

Configure JPG Rendering

The ImageSaveOptions class controls how the archived page is rendered. Use it to set horizontal and vertical resolution, page size and margins, background color, antialiasing, CSS processing, and text rendering. JPG uses lossy compression and does not support transparency. Choose MHTML to PNG when lossless text detail or transparent output is more important.

For complete settings and examples, see Convert MHTML to JPG in Java and the ImageSaveOptions API Reference .


Install Aspose.HTML for Java

Add Aspose.HTML for Java from the official Aspose repository. Replace ASPOSE_HTML_VERSION with an exact version from the Aspose.HTML for Java releases page, then reload or rebuild the project so the dependency is resolved.

Add the repository and dependency to the Maven project:

Maven – pom.xml

<repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <url>https://releases.aspose.com/java/repo/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-html</artifactId>
        <version>ASPOSE_HTML_VERSION</version>
    </dependency>
</dependencies>

Add the repository and dependency to the Gradle project:

Gradle – build.gradle

repositories {
    maven {
        url = uri('https://releases.aspose.com/java/repo/')
    }
}
dependencies {
    implementation 'com.aspose:aspose-html:ASPOSE_HTML_VERSION'
}

Use a JDK compatible with the selected release. For manual JAR installation, Kotlin DSL, updates, and dependency troubleshooting, see Install Aspose.HTML for Java .


MHTML to JPG Conversion FAQ

1. Does MHTML to JPG conversion preserve links and interactive content?

No. JPG is a static raster format. Links, forms, scripts, audio, video, and other interactive MHTML content are rendered visually but do not remain interactive.

2. Does Aspose.HTML for Java support both .mht and .mhtml files?

Yes. Both extensions identify MHTML web archives and can be supplied to the conversion workflow through a file stream or another supported source overload.

3. Can I change the resolution of the JPG created from MHTML?

Yes. Call setHorizontalResolution() and setVerticalResolution() on ImageSaveOptions before Converter.convertMHTML(). The same options object also controls page size, margins, background color, and rendering behavior.

4. Can I convert MHTML to JPG online for free?

Yes. Use the free online MHTML to JPG converter for a manual conversion. Aspose.HTML for Java is the commercial API for integrating the same task into Java applications and can be evaluated before licensing.

Related Java Conversions

Continue with another source or output format in Aspose.HTML for Java.