You can find more details at: Convert XPS to JPG or PNG Image in Java
Last active
March 16, 2022 08:17
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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