XPS (XML Paper Specifications) format was introduced by Microsoft that is used to represent page layout. It uses XML tags to represent the appearance of the pages and the composition of the document. In various scenarios, you may need to convert XPS documents to other document formats. In accordance with that, in this article, you will learn about the conversion of XPS documents to raster image formats programmatically. Particularly, the article will cover how to convert XPS to BMP, JPEG, PNG, and TIFF using Java.

XPS to Image Converter API - Free Download

Aspose.Page for Java is designed to work with PS, EPS, and XPS documents from within the Java applications. The API’s built-in converter lets you perform high-quality conversion of XPS to raster image formats including PNG, JPEG, BMP, and TIFF images. You can either download the API’s JAR or install it within your Maven-based applications.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-page</artifactId>
    <version>20.11</version>
</dependency>

Convert XPS to Raster Images in Java

Aspose.Page for Java provides separate classes in order to customize the XPS to raster image conversion. For example, you can set the resolution of the output image, specify the pages you want to convert, and so on. The following is the list of the classes that you may use accordingly.

Convert XPS to PNG in Java

The following are the steps to convert the XPS documents to PNG images using Aspose.XPS for Java.

The following code sample shows how to convert XPS to PNG using Java.

// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.PngSaveOptions options = new com.aspose.xps.rendering.PngSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document as images bytes array. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.saveAsImage(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}

Convert XPS to JPEG in Java

The following are the steps to convert XPS to JPEG using Aspose.Page for Java.

The following code sample shows how to convert XPS to JPEG.

// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize XPS input stream
// Load XPS document form the stream
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.JpegSaveOptions options = new com.aspose.xps.rendering.JpegSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document as images bytes array. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.saveAsImage(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}

Convert XPS to BMP in Java

You can convert the XPS files to BMP images in the same way you have done for PNG and JPEG. The following are the steps for it.

The following code sample shows how to convert an XPS file to BMP.

// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.BmpSaveOptions options = new com.aspose.xps.rendering.BmpSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[]{1, 2, 6});
// Save XPS document as images bytes array. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.saveAsImage(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}

Convert XPS to TIFF in Java

The following are the steps to convert XPS document to TIFF image.

The following code sample shows how to convert XPS document to TIFF image in Java.

// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.TiffSaveOptions options = new com.aspose.xps.rendering.TiffSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document as images bytes array. The first dimension is for inner documents
// and the second one is for pages within inner documents.
byte [][][] imagesBytes = document.saveAsImage(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}

Conclusion

In this article, you have learned how to convert XPS files to raster image formats using Java. The step-by-step guide, API references, and code samples have shown how to convert XPS to PNG, JPEG, TIFF, and BMP images. You can explore other features of the Java XPS API using the documentation.

See Also