This tutorial covers basic information about how to convert EPS to PNG image using Java. You can export EPS to PNG image in Java using simple API calls on any operating system like Linux, MS Windows and macOS. You can create PNG image with high fidelity as it can be helpful for previewing the Encapsulated PostScript (EPS) files without needing any specific tool or application.
Steps to Convert EPS to PNG Image using Java
- Configure your project by specifying the Aspose.Page JAR file from the Maven Repository
- Initialize output image format as PNG
- Initialize PsDocument class object with input EPS file
- Create an ImageDevice class object with needed image dimensions
- Save the generated PNG image to the disk
In order to generate PNG from EPS using Java, first we will specify the output format using ImageFormat enumeration. Using FileInputStream class, we will load the input EPS file and initialize an object of PsDocument class. Then we will use ImageDevice class with needed image dimensions and save the output PNG image to disk using Java.
Code to Convert EPS to PNG Image using Java
import com.aspose.eps.License; | |
import com.aspose.eps.ImageFormat; | |
import com.aspose.eps.PsDocument; | |
import com.aspose.eps.device.ImageDevice; | |
import com.aspose.eps.device.ImageSaveOptions; | |
import java.awt.Dimension; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
public class ConvertEPStoPNG { | |
public static void main(String[] ePSArgs) throws Exception { // main method for converting EPS to PNG | |
// Set Aspose.Page license before converting EPS to PNG in Java | |
License pageLicense = new License(); | |
pageLicense.setLicense("Aspose.Page.lic"); | |
//Initialize output image format as PNG | |
ImageFormat targetImageFormat = ImageFormat.PNG; | |
// Initialize FileInputStream to load input EPS file | |
FileInputStream psStream = new FileInputStream("input.eps"); | |
// Initialize PsDocument class object with loaded EPS file | |
PsDocument document = new PsDocument(psStream); | |
// Set the flag if you want to convert Postscript file ignoring minor errors | |
boolean suppressErrors = true; | |
// Initialize options object with required parameters. | |
ImageSaveOptions options = new ImageSaveOptions(suppressErrors); | |
// Initialize ImageDevice class object with needed image dimensions | |
ImageDevice ePSImageDevice = new ImageDevice(new Dimension(595, 842), targetImageFormat); | |
try { | |
document.save(ePSImageDevice, options); | |
} finally { | |
psStream.close(); | |
} | |
byte[][] imagesBytes = ePSImageDevice.getImagesBytes(); | |
int i = 0; | |
for (byte [] imageBytes : imagesBytes) { | |
String imagePath = "EPSToImage" + 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++; | |
} | |
} | |
} |
In the previous topic, we have learnt how to create HTML file using Java. Whereas, this topic about Java create PNG from EPS can be helpful for previewing the EPS files or generating thumbnails for displaying the contents.