I have this test code that exports PSD layers as PNG images, but by default it seems the tool exports the entire layer. What I’ve been unable to find in the documentation is a way to crop the exported layer to the same size as the PSD artboard. In the example PSD I’ll attach, you can see the entire circle is visible even though the artboard is too small to contain that shape.
Is this something Aspose PSD supports?
// Various imports, mostly from aspose-psd-21.7-jdk16.jar
import com.aspose.psd.Image;
import com.aspose.psd.fileformats.psd.PsdImage;
import com.aspose.psd.imageoptions.PngOptions;
import com.aspose.psd.fileformats.png.PngColorType;
import com.aspose.psd.fileformats.psd.layers.Layer;
import com.aspose.psd.fileformats.psd.layers.SectionDividerLayer;
import com.aspose.psd.fileformats.psd.layers.LayerGroup;
// Core Java imports
import java.io.File;
import java.nio.file.Files;
// Main class
// javac -cp .:aspose-psd-21.7-jdk16.jar Layers.java
// java -cp .:aspose-psd-21.7-jdk16.jar Layers
class Layers {
// Helper to delete a directory that may have files in it
public static void deleteDir(File file) {
File[] contents = file.listFiles();
if (contents != null) {
for (File f: contents) {
if (!Files.isSymbolicLink(f.toPath())) {
deleteDir(f);
}
}
}
file.delete();
}
// Given a string name of a PSD file save all its layers to a directory named "image"
public static void saveLayers(String strPSD) {
// If the "image" directory exists then delete and recreate it
// This directory is where we'll store our work
File dirImage = new File("image");
if (dirImage.exists()) {
deleteDir(dirImage);
}
dirImage.mkdir();
// Load a PSD file as an image and caste it into PsdImage
PsdImage psdImage = (PsdImage) Image.load(strPSD);
// Create an instance of PngOptions class
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
// Declare a saved layer counter
int iSavedLayerCounter = 0;
// Grab the layers from the PSD
Layer arLayers[] = psdImage.getLayers();
// Loop through the layers
for (int i = 0; i < arLayers.length; i++) {
// Grab a handle to this layer iteration
Layer objLayer = arLayers[i];
// If this layer is a section divider or a group then ignore it
if (objLayer instanceof SectionDividerLayer || objLayer instanceof LayerGroup) continue;
// Print the name of this layer
System.out.println(objLayer.getName());
String strSavedLayerName = "./image/" + String.format("%02d", iSavedLayerCounter++) + "_layer.png";
// Write this layer to disk
objLayer.save(strSavedLayerName, pngOptions);
}
}
// Main entry point, args is currently ignored
public static void main(String[] args) {
saveLayers("f220215_sm_vg_category_hero.psd");
}
}