Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 16, 2022 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/bdff358fc524d4036ef2cbece52b9363 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/bdff358fc524d4036ef2cbece52b9363 to your computer and use it in GitHub Desktop.
Convert XPS to JPG or PNG Image Programmatically in Java | XPS to JPEG Converter
// Load XPS document form the stream
XpsDocument document = new XpsDocument("sample.xps");
// Initialize options object with necessary parameters.
JpegSaveOptions options = new JpegSaveOptions();
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1 , 3 });
// Create rendering device for image
ImageDevice device = new ImageDevice();
document.save(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < device.getResult().length; i++) {
// Iterate through partition pages
for (int j = 0; j < device.getResult()[i].length; j++) {
// Initialize image output stream
FileOutputStream imageStream = new FileOutputStream("XPStoJPEG" + "_" + (i + 1) + "_" + (j + 1) + ".jpeg");
// Write image
imageStream.write(device.getResult()[i][j], 0, device.getResult()[i][j].length);
}
}
// Load XPS document form the stream
XpsDocument document = new XpsDocument("sample.xps");
// Initialize options object with necessary parameters.
PngSaveOptions options = new PngSaveOptions();
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1 , 3 });
// Create rendering device for image
ImageDevice device = new ImageDevice();
document.save(device, options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < device.getResult().length; i++) {
// Iterate through partition pages
for (int j = 0; j < device.getResult()[i].length; j++) {
// Initialize image output stream
FileOutputStream imageStream = new FileOutputStream("XPStoPNG" + "_" + (i + 1) + "_" + (j + 1) + ".png");
// Write image
imageStream.write(device.getResult()[i][j], 0, device.getResult()[i][j].length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment