Converting XPS documents to raster images is a frequent need when building preview features or generating thumbnails. Conholdate.Total for Java enables developers to perform XPS to JPG in Java with just a few lines of code. In this guide you’ll see a complete code example, learn how to configure resolution and JPEG quality, and discover best practices for handling multi-page XPS files.
XPS to JPG Java Sample - Complete Code Example
This example demonstrates how to convert an XPS document to JPEG images using Conholdate.Total for Java.
import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.ImageConvertOptions;
import java.io.File;
public class XpsToJpgConverter {
public static void main(String[] args) {
// Input XPS file and output directory
String inputPath = "sample.xps";
String outputDir = "output_images";
// Conversion settings
int resolutionDpi = 300; // Desired DPI
int jpegQuality = 90; // JPEG quality (0‑100)
// Ensure output directory exists
File outFolder = new File(outputDir);
if (!outFolder.exists()) {
outFolder.mkdirs();
}
// Perform conversion
try (Converter converter = new Converter(inputPath)) {
ImageConvertOptions options = new ImageConvertOptions();
options.setFormat(ImageConvertOptions.ImageFormat.JPEG);
options.setResolution(resolutionDpi);
options.setQuality(jpegQuality);
int pageCount = converter.getPageCount();
for (int page = 1; page <= pageCount; page++) {
options.setPageNumber(page);
String outputPath = outputDir + File.separator + "page_" + page + ".jpg";
converter.convert(outputPath, options);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
How XPS Conversion to JPG Works in Java
The conversion process can be broken down into a few clear steps:
Initialize the Converter - The
Converterclass is instantiated with the path to the XPS file.try (Converter converter = new Converter(inputPath)) {Create ImageConvertOptions - An
ImageConvertOptionsobject defines the target format (JPEG), resolution, and quality.ImageConvertOptions options = new ImageConvertOptions(); options.setFormat(ImageConvertOptions.ImageFormat.JPEG);Determine Page Count -
converter.getPageCount()returns the number of pages in the XPS document, enabling per‑page processing.int pageCount = converter.getPageCount();Loop Through Pages - For each page, set
options.setPageNumber(page)and callconverter.convert(outputPath, options)to generate a JPEG file.for (int page = 1; page <= pageCount; page++) { options.setPageNumber(page); converter.convert(outputPath, options); }Handle Exceptions - Any conversion errors are caught and printed, allowing you to debug issues such as missing fonts or unsupported XPS features.
For deeper details, see the API reference for Converter and ImageConvertOptions.
XPS to JPG Conversion - Prerequisites and Setup
Before you start coding, make sure your development environment meets the following requirements:
- Java 8 or higher
- Maven for dependency management
- Access to a valid Conholdate.Total for Java license for production use
Add the Conholdate Maven repository and the SDK dependency to your pom.xml:
<repositories>
<repository>
<id>conholdate-repo</id>
<name>Conholdate Maven Repository</name>
<url>https://repository.conholdate.com/repo/</url>
</repository>
</repositories>
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.9</version>
<type>pom</type>
</dependency>
Download the latest SDK binaries from the download page. After adding the dependency, run mvn clean install to resolve all required libraries.
Configuration Options for XPS to JPG Conversion
The sample code already sets two important options, but you can fine‑tune additional parameters:
Resolution (DPI) - Controls the output image clarity. Higher DPI yields sharper images but larger file sizes.
options.setResolution(300); // 300 DPI is standard for print qualityJPEG Quality - Ranges from 0 to 100. A value of 90 provides a good balance between compression and visual fidelity.
options.setQuality(90);Output Directory - Ensure the target folder exists before conversion to avoid
IOException.File outFolder = new File(outputDir); if (!outFolder.exists()) { outFolder.mkdirs(); }Page Number - Set the current page to convert. This allows you to extract images from XPS to JPG in Java on a per‑page basis, supporting multi‑page workflows.
Adjusting these settings lets you optimize the XPS to JPG workflow for different use cases, whether you need fast batch conversion or high‑quality thumbnails.
Wrapping Up
You now have a complete, production‑ready example for converting XPS to JPG in Java using Conholdate.Total for Java. By configuring resolution, JPEG quality, and handling each page individually, you can integrate this XPS to JPG conversion into larger document processing pipelines, web services, or desktop applications. Remember to obtain a proper license for commercial deployments; you can request a temporary license at the temporary license page and review pricing options at the pricing page. Happy coding!
Frequently Asked Questions
How can I convert XPS to JPG in Java?
Use the Converter class from Conholdate.Total for Java. Set ImageConvertOptions to JPEG, configure resolution and quality, then call convert for each page.
Is it possible to batch convert multiple XPS files to JPG in Java?
Yes. Place each XPS file in a loop, reuse a single Converter instance, and change the input path for each iteration. This approach minimizes memory usage and speeds up the XPS to JPG conversion process.
What settings should I use for high‑quality image output?
A resolution of 300 DPI and a JPEG quality of 90 are recommended for most scenarios. Adjust these values in ImageConvertOptions to meet specific size or quality requirements.
Where can I find more information about licensing and support?
Visit the temporary license page for a trial license and the pricing page for full‑license options. For technical assistance, the support team is available to help.
