Manipulating EPS Images

Export EPS images to other formats

         PostScript documents have been adopted for combining text, graphics, and images into graphic files since the 1980s. The combined data is saved in the Encapsulated Postscript format (EPS). EPS files are supported by many vector editing applications and remain popular for creating graphic documents. With the Aspose.Imaging graphic library for Java, you can rapidly develop applications or services to manipulate EPS images.

         You can easily load EPS files and convert them into other image vector or raster formats such as SVG, PNG, PDF, or PSD. In the Java code example provided, we export EPS images to SVG and PNG formats by using the Save method on the loaded EPS image object. For converting to the PNG format, we specify PngColorType.Grayscale as an additional option for a Grayscale PNG palette.

import com.aspose.imaging.Image;
import com.aspose.imaging.ImageOptionsBase;
import com.aspose.imaging.imageoptions.PngOptions;
import com.aspose.imaging.fileformats.png.PngColorType;
import java.io.File;
import java.util.HashMap;
String dataDir = "c:\\Users\\USER\\Downloads\\templates\\";
HashMap<String, ImageOptionsBase> outputOptions = new HashMap<String, ImageOptionsBase>();
outputOptions.put(dataDir + "output.svg", null);
outputOptions.put(dataDir + "output.png", new PngOptions() {{ setColorType(PngColorType.Grayscale); }});
try (Image image = Image.load(dataDir + "template.eps"))
{
for (String kv : outputOptions.keySet())
{
ImageOptionsBase options = outputOptions.get(kv);
if (options == null)
{
image.save(kv);
}
else
{
image.save(kv, options);
}
// dispose the options
if (options != null)
{
options.close();
}
}
}
for (String kv : outputOptions.keySet())
{
new File(kv).delete();
}

         To export the loaded EPS image to a PDF document, utilize the option parameter `pdfCoreOptions` and specify the required PDF compliance version, such as `PdfA1b`, when saving the PDF file:

import com.aspose.imaging.Image;
import com.aspose.imaging.PdfComplianceVersion;
import com.aspose.imaging.fileformats.eps.EpsImage;
import com.aspose.imaging.fileformats.pdf.PdfCoreOptions;
import com.aspose.imaging.imageoptions.PdfOptions;
import java.io.File;
String dataDir = "c:\\Users\\USER\\Downloads\\templates\\";
try (EpsImage image = (EpsImage) Image.load(dataDir + "template.eps"))
{
PdfOptions options = new PdfOptions();
final PdfCoreOptions pdfCoreOptions = new PdfCoreOptions();
// Set required PDF compliance
pdfCoreOptions.setPdfCompliance(PdfComplianceVersion.PdfA1b);
options.setPdfCoreOptions(pdfCoreOptions);
image.save(dataDir + "result.pdf", options);
}
new File(dataDir + "result.pdf").delete();

Resize EPS images before export

         When working with EPS images, you can perform various operations, such as resizing before exporting to another file format. Apply the `Resize` method to the loaded EPS image object to change the image width and height:

import com.aspose.imaging.Image;
import com.aspose.imaging.ImageOptionsBase;
import com.aspose.imaging.imageoptions.PngOptions;
import com.aspose.imaging.fileformats.png.PngColorType;
import java.io.File;
import java.util.HashMap;
String dataDir = "c:\\Users\\USER\\Downloads\\templates\\";
HashMap<String, ImageOptionsBase> outputOptions = new HashMap<String, ImageOptionsBase>();
outputOptions.put(dataDir + "output.svg", null);
outputOptions.put(dataDir + "output.png", new PngOptions() {{ setColorType(PngColorType.Grayscale); }});
try (Image image = Image.load(dataDir + "template.eps"))
{
image.resize(image.getWidth() * 2, image.getHeight() * 2);
for (String kv : outputOptions.keySet())
{
ImageOptionsBase options = outputOptions.get(kv);
if (options == null)
{
image.save(kv);
}
else
{
image.save(kv, options);
}
// dispose the options
if (options != null)
{
options.close();
}
}
}
for (String kv : outputOptions.keySet())
{
new File(kv).delete();
}

Extract preview image from the EPS files

         Typically, EPS files also contain a low-resolution image preview embedded in the document, enabling other applications to display it. The preview can be stored in various formats within the file. To extract and save the preview image in TIFF, WMF, or JPEG format from an EPS file, utilize the EpsPreviewFormat parameter with the with getPreviewImages() method:

import com.aspose.imaging.Image;
import com.aspose.imaging.fileformats.eps.EpsImage;
import com.aspose.imaging.fileformats.eps.EpsPreviewFormat;
import java.io.ByteArrayOutputStream;
String dataDir = "c:\\Users\\USER\\Downloads\\templates\\";
byte[] tiffPreviewByteArray = null;
try (EpsImage image = (EpsImage) Image.load(dataDir + "template.eps"))
{
Image tiffPreview = image.getPreviewImage(EpsPreviewFormat.TIFF);
if (tiffPreview != null)
{
try (ByteArrayOutputStream tiffPreviewStream = new ByteArrayOutputStream())
{
tiffPreview.save(tiffPreviewStream);
tiffPreviewByteArray = tiffPreviewStream.toByteArray();
}
}
}
if (tiffPreviewByteArray != null)
{
// Do something
// And release the memory
tiffPreviewByteArray = null;
}