Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created October 27, 2023 14:46

Aspose.Imaging for Java API allows to easy manipulate your EPS images in your Java application or Web service.

You can:

  • Export EPS image to other formats;
  • Resize EPS image before export;
  • Extract preview images from EPS image.

Interested ?

Go further at : https://products.aspose.com/imaging/java/

import com.aspose.imaging.FileFormat;
import com.aspose.imaging.Image;
import com.aspose.imaging.fileformats.eps.EpsImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
String dataDir = "c:\\Users\\USER\\Downloads\\templates\\";
List<String> epsPreviewFiles = new ArrayList<String>();
try (EpsImage image = (EpsImage) Image.load(dataDir + "template.eps"))
{
for (Image preview : image.getPreviewImages())
{
String previewPath = dataDir + "output." +
FileFormat.toString(FileFormat.class, preview.getFileFormat()).toLowerCase();
preview.save(previewPath);
epsPreviewFiles.add(previewPath);
}
}
for (String file : epsPreviewFiles)
{
new File(file).delete();
}
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();
}
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;
}
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment