Convert EPUB to XPS in Java

XPS is a document storage and viewing format developed by Microsoft. An XPS file has a set of advantages that support security features, such as digital signatures to provide greater document security and more. With Aspose.HTML for Java, you can convert EPUB to XPS format programmatically with full control over a wide range of conversion parameters.

In this article, you will find information on how to convert an EPUB to XPS with Aspose.HTML for Java and how to use XpsSaveOptions and MemoryStreamProvider parameters.

EPUB to XPS by a single line of Java code

The static methods of the Converter class are primarily used as the easiest way to convert an EPUB file into various formats. You can convert EPUB to XPS in your Java application literally with a single line of code!

1    // Invoke the convertEPUB()
2    com.aspose.html.converters.Converter.convertEPUB(File.openRead(StringExtensions.concat(getDataDir(),  "input.epub")), new XpsSaveOptions(), Path.combine(getOutputDir(), "output.xps"));

Convert EPUB to XPS

Using convertEPUB() methods is the most common way to convert EPUB files into various formats. The following Java code snippet shows how to convert EPUB to XPS using Aspose.HTML:

  1. Open an existing EPUB file. In the example, we use the openRead() method to open and read an EPUB file from the file system at the specified path.
  2. Create an instance of XpsSaveOptions. Use the empty XpsSaveOptions() constructor to convert with the default save options.
  3. Use the convertEPUB(stream, options, savePath) method of the Converter class to save EPUB as an XPS file. You need to pass the EPUB file stream, XpsSaveOptions, and output file path to the convertEPUB() method as parameters.
 1    // Open an existing EPUB file for reading
 2    final  FileStream stream = File.openRead(StringExtensions.concat(getDataDir(),  "input.epub"));
 3    try {        }
 4    finally { if (stream != null) ((IDisposable)stream).dispose(); }
 5
 6    // Prepare a path to save the converted file 
 7    String savePath = Path.combine(getOutputDir(), "input-output.xps");
 8
 9    // Create an instance of XpsSaveOptions
10    XpsSaveOptions options = new XpsSaveOptions();
11
12    // Call the convertEPUB() method
13    com.aspose.html.converters.Converter.convertEPUB(stream, options, savePath);

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

Save Options

Aspose.HTML provides the ability to programmatically convert EPUB to XPS with full control over various conversion options using XpsSaveOptions. This feature allows users to customize the rendering process by setting the page size, margins, CSS media-type, and other parameters.

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.

Convert EPUB to XPS using XpsSaveOptions

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

  1. Open an existing EPUB file. Use the openRead() method of System.IO.FileStream class to open and read source files from the file system at the specified path.
  2. Create a new XpsSaveOptions object and specify the required save options. Use the setPageSetup() and setBackgroundColor() methods to set the page size and background color.
  3. Use the convertEPUB(stream, options, savePath) method to save EPUB as an XPS file. The method takes the stream, options, output file path savePath and performs the conversion operation.

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

 1    // Open an existing EPUB file for reading
 2    final  FileStream stream = File.openRead(StringExtensions.concat(getDataDir(),  "input.epub"));
 3    try {        }
 4    finally { if (stream != null) ((IDisposable)stream).dispose(); }
 5
 6    // Prepare a path to save the converted file 
 7    String savePath = Path.combine(getOutputDir(), "input-options.xps");
 8
 9    // Create an instance of XpsSaveOptions. Set up the page-size and change the background color to LightGray
10    XpsSaveOptions options = new XpsSaveOptions();
11    PageSetup pageSetup = new PageSetup();
12    Page anyPage = new Page();
13    anyPage.setSize(new com.aspose.html.drawing.Size(Length.fromPixels(500),Length.fromPixels(500)));
14    pageSetup.setAnyPage(anyPage);
15    options.setPageSetup(pageSetup);
16    options.setBackgroundColor(Color.getLightGray());
17
18    // Call the convertEPUB() method
19    com.aspose.html.converters.Converter.convertEPUB(stream, options, savePath);

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

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.

Aspose.HTML offers a free online EPUB to XPS Converter that converts EPUB to XPS 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.