How to Convert EPS to JPG in Java

This short tutorial explains how to convert EPS to JPG in Java. It includes the details pertaining to environment setup, and stepwise process along with a runnable code snippet to change EPS to JPG in Java. Moreover, this process does not require installing any additional tool or dependency for converting EPS format to a JPG image.

Steps to Convert EPS to JPG using Java

  1. Configure Aspose.Page for Java API in your environment to render EPS files
  2. Initialize a FileInputStream class object to access the input EPS file
  3. Load the source EPS file with the PsDocument class
  4. Create an instance of ImageSaveOptions class and specify image properties
  5. Instantiate an image rendering device to create a JPG image
  6. Convert the EPS document to a JPG image

These steps help you understand how to transform EPS to JPG in Java. Simply install the library in your system and then process input EPS files with a few API calls. Finally, specify image properties for expected output and process as many PostScript files as you may need.

Code to Convert EPS to JPG in Java

import java.io.IOException;
import com.aspose.page.License;
public class AsposeTest {
public static void main(String[] args) throws Exception {//Main function to convert EPS to JPG using Java
// Instantiate the license
License lic = new License();
lic.setLicense("Aspose.Total.lic");
// Load input EPS file
java.io.FileInputStream psStream = new java.io.FileInputStream("input.eps");
// Create PsDocument class object
com.aspose.eps.PsDocument document = new com.aspose.eps.PsDocument(psStream);
// Initialize options object.
com.aspose.eps.device.ImageSaveOptions options = new com.aspose.eps.device.ImageSaveOptions();
// Initialize ImageDevice class object
com.aspose.eps.device.ImageDevice device = new com.aspose.eps.device.ImageDevice();
try{
document.save(device, options);
}finally{
psStream.close();}
byte[][] imagesBytes = device.getImagesBytes();
int i = 1;
// Write output JPG image
for (byte[] imageBytes : imagesBytes)
{
String imagePath = "EPStoJPG_" + i + ".jpg";
java.io.FileOutputStream fs = new java.io.FileOutputStream(imagePath);
try {
fs.write(imageBytes, 0, imageBytes.length);
} catch (IOException ex) {
System.out.println(ex.getMessage());
} finally {
fs.close();
}
i++;
}
System.out.println("Done");
}
}

This code snippet can be used as it is to render the EPS files to JPG images. However, you can modify different characteristics of the output image while exporting from EPS to JPG in Java. For instance, you can choose to suppress minor errors, set the dimensions for the output image, or adjust the resolution and other properties exposed by the ImageSaveOptions class.

This tutorial has discussed how to convert an EPS file to JPG in Java. Furthermore, if you want to render an EPS file to a PNG image, please read the article on how to convert EPS to PNG in Java.

 English