Open PSD, PSB and AI files and export them to PDF, PNG, TIFF, GIF, BMP, JPEG
Overview
To convert PSD, PSB, and AI files to different formats using Java, you can utilize the Aspose.PSD library. Here’s an overview of the steps involved:
-
Import the necessary classes and modules from the Aspose.PSD library.
-
Define various options for different formats such as PNG, PDF, TIFF, JPEG, BMP, JPEG2000, GIF, PSB, and PSD.
-
Create a dictionary that maps file extensions to their respective save options.
-
Load the PSD file using PsdImage.load() and iterate through the formats dictionary to save the image in each desired format.
-
Similarly, load the AI file using AiImage.load() and iterate through the formats dictionary to save the image in each desired format.
Ensure to provide correct file paths for the source PSD and AI files. That’s the general procedure for converting PSD, PSB, and AI files to various formats using Aspose.PSD for Java.
Please check full example.
Example
public void exportPsdAndAIToDifferentFormatsTest() { | |
// Saving to PNG | |
PngOptions pngSaveOpt = new PngOptions(); | |
pngSaveOpt.setColorType(PngColorType.TruecolorWithAlpha); | |
// Saving to PDF | |
PdfOptions pdfSaveOpt = new PdfOptions(); | |
// Saving to Tiff | |
TiffOptions tiffSaveOpt = new TiffOptions(TiffExpectedFormat.TiffNoCompressionRgba); | |
// Saving to Jpeg | |
JpegOptions jpegSaveOpt = new JpegOptions(); | |
jpegSaveOpt.setQuality(90); | |
// Saving to BMP | |
BmpOptions bmpSaveOpt = new BmpOptions(); | |
// Saving to JPEG2000 | |
Jpeg2000Options j2kSaveOpt = new Jpeg2000Options(); | |
// Saving to GIF | |
GifOptions gifSaveOpt = new GifOptions(); | |
// Saving to PSB | |
PsdOptions psbSaveOpt = new PsdOptions(); | |
psbSaveOpt.setVersion(2); | |
// Saving to PSD | |
PsdOptions psdSaveOpt = new PsdOptions(); | |
Map<String, ImageOptionsBase> formats = new HashMap<>(); | |
formats.put("pdf", pdfSaveOpt); | |
formats.put("jpg", jpegSaveOpt); | |
formats.put("png", pngSaveOpt); | |
formats.put("tif", tiffSaveOpt); | |
formats.put("gif", gifSaveOpt); | |
formats.put("j2k", j2kSaveOpt); | |
formats.put("bmp", bmpSaveOpt); | |
formats.put("psb", psbSaveOpt); | |
formats.put("psd", psdSaveOpt); | |
// Saving PSD to other formats | |
String sourcePsd = "AllTypesLayerPsd2.psd"; | |
try (PsdImage image = (PsdImage) com.aspose.psd.Image.load(sourcePsd)) { | |
for (Map.Entry<String, ImageOptionsBase> entry : formats.entrySet()) { | |
String format = entry.getKey(); | |
ImageOptionsBase saveOpt = entry.getValue(); | |
String fn = "export.psd.to." + format; | |
image.save(fn, saveOpt); | |
} | |
} | |
// Saving AI to other formats | |
String sourceAi = "ai_one_text_3.ai"; | |
try (AiImage image = (AiImage) com.aspose.psd.Image.load(sourceAi)) { | |
for (Map.Entry<String, ImageOptionsBase> entry : formats.entrySet()) { | |
String format = entry.getKey(); | |
ImageOptionsBase saveOpt = entry.getValue(); | |
String fn = "export.ai.to." + format; | |
image.save(fn, saveOpt); | |
} | |
} | |
} |