Convert MHTML to XPS in Java

XPS is a fixed-layout document format that can be viewed on any device with an XPS viewer installed. It allows for high-quality printing and supports various page layouts and typography features. Therefore, if you want to create a document that can be easily shared and printed, converting MHTML to XPS may be a good option.

In this article, you will find information on how to convert an MHTML to XPS using Aspose.HTML for Java library and how to apply XpsSaveOptions and MemoryStreamProvider.

MHTML to XPS by a few lines of code

The static methods of the Converter class are primarily used as the easiest way to convert an MHTML file into various formats. You can convert MHTML to XPS with just a few lines of code!

1    // Open an existing MHTML file for reading
2    final  com.aspose.html.internal.ms.System.IO.FileStream stream = com.aspose.html.internal.ms.System.IO.File.openRead(StringExtensions.concat(getDataDir(),  "sample.mht"));
3    try
4    {;
5    }
6    finally { if (stream != null) stream.dispose(); }
7
8    // Invoke the convertMHTML() method to convert MHTML           
9    com.aspose.html.converters.Converter.convertMHTML(stream, new XpsSaveOptions(), Path.combine(getOutputDir(), "convert-by-few-lines.xps"));

Convert MHTML to XPS in Java

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

  1. Load an MHTML file. You can load MHTML from a file, stream, or URL. In the example we use openRead(path) method to open and read an MHTML document from the file system.
  2. Create a new XpsSaveOptions object. Use the empty XpsSaveOptions() constructor to convert with the default save options.
  3. Use the сonvertMHTML(stream, options, savePath) method of the Converter class to save an MHTML document as an XPS file. The method takes as parameters stream, options, and savePath and performs the conversion.
 1    // Open an existing MHTML file for reading
 2    final  com.aspose.html.internal.ms.System.IO.FileStream stream = com.aspose.html.internal.ms.System.IO.File.openRead(StringExtensions.concat(getDataDir(),  "sample.mht"));
 3    try
 4    {;
 5    }
 6    finally { if (stream != null) stream.dispose(); }
 7
 8    // Prepare a path for converted file saving 
 9    String savePath = Path.combine(getOutputDir(), "sample-output.xps");
10
11    // Create an instance of XpsSaveOptions
12    XpsSaveOptions options = new XpsSaveOptions();
13
14    // Convert MHTML to XPS
15    com.aspose.html.converters.Converter.convertMHTML(stream, options, savePath);

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

Save Options

XpsSaveOptions allows you to personalize the rendering process. This includes customizing properties such as the page size, margins, background color, CSS media type, and more.

MetodDescription
getCss()Gets a CssOptions object which is used for configuration of CSS properties processing.
setBackgroundColor(value)Sets the color that will fill the background of every page. By default, this property is Transparent.
setPageSetup(value)This method sets a page setup object and uses it for configuration output page-set.
setHorizontalResolution(value)Sets horizontal resolution for internal images, in pixels per inch. By default this property is 300 dpi.
setVerticalResolution(value)Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi.

Please note that you cannot set values against the Application and Producer fields, because Aspose Ltd. and Aspose.HTML for Java x.x.x will be displayed against these fields.

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

Convert MHTML to XPS in Java using XpsSaveOptions

The XpsSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting MHTML to XPS format.

  1. Load an MHTML file. You can load MHTML from a file, stream, or URL.
  2. Create a new XpsSaveOptions object and set the required save options:
    • Use the setPageSetup() method to specify the page size for the output XPS document.
    • Use the setBackgroundColor() method to set the color that fills the background.
  3. Use the convertHTML() method of the Converter class to save HTML as an XPS file. You must pass parameters to it – document, options and output file path savePath to perform the conversion.

The following example shows how to use XpsSaveOptions and create an XPS file with custom page size and background color:

 1    // Open an existing MHTML file for reading
 2    final  com.aspose.html.internal.ms.System.IO.FileStream stream = com.aspose.html.internal.ms.System.IO.File.openRead(StringExtensions.concat(getDataDir(),  "sample.mht"));
 3    try
 4    {;
 5    }
 6    finally { if (stream != null) stream.dispose(); }
 7
 8    // Prepare a path for converted file saving 
 9    String savePath = Path.combine(getOutputDir(), "sample-options.xps");
10
11    // Create an instance of XpsSaveOptions. Set up the page-size and change the background color to AliceBlue 
12    XpsSaveOptions options = new XpsSaveOptions();
13    options.getPageSetup().setAnyPage(new Page(new com.aspose.html.drawing.Size(com.aspose.html.drawing.Length.fromInches(8.3f), com.aspose.html.drawing.Length.fromInches(5.8f))));
14    com.aspose.html.drawing.Color.getAliceBlue().CloneTo(options.getBackgroundColor());
15
16    // Convert MHTML to XPS
17    com.aspose.html.converters.Converter.convertMHTML(stream, options, savePath);

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

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

Text “Banner MHTML to XPS Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.