Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 5, 2022 19:48
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/c02f65a7a2171aac01c19128ebffd5b4 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/c02f65a7a2171aac01c19128ebffd5b4 to your computer and use it in GitHub Desktop.
Convert EPS to PNG in Java | PostScript EPS to PNG in Java
// This code example demonstrates how to convert EPS to PNG.
// Load file in input stream
FileInputStream psStream = new FileInputStream("C:\\Files\\input.eps");
// Instantiate PS document
PsDocument document = new PsDocument(psStream);
// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;
// Initialize options object with necessary parameters.
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
// Optionally, if you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
//options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});
// Initialize image format as PNG
ImageFormat imageFormat = ImageFormat.PNG;
// Default image format is PNG and it is not mandatory to set it in ImageDevice
// Default image size is 595x842 and it is not mandatory to set it in ImageDevice
com.aspose.eps.device.ImageDevice device = new com.aspose.eps.device.ImageDevice();
// But if you need to specify size and image format use constructor with parameters
//ImageDevice device = new ImageDevice(new Dimension(595, 842), com.aspose.eps.ImageFormat.Jpeg);
try {
// Save to device
document.save(device, options);
} finally {
psStream.close();
}
// Get image bytes
byte[][] imagesBytes = device.getImagesBytes();
int i = 0;
// Save to disk
for (byte [] imageBytes : imagesBytes) {
String imagePath = "C:\\Files\\PSToImage" + i + "." + imageFormat.toString().toLowerCase();
FileOutputStream fs = new FileOutputStream(imagePath);
try {
fs.write(imageBytes, 0, imageBytes.length);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
fs.close();
}
i++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment