We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Export PSD layers as separate PNG files, while retaining artboard cropping

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");
    }
}

f210824_email_image_hero_cm_BU_SW_15P_NoMin.psd.zip (905.6 KB)

@tcaruth,
If I correctly understand, you want the result images will have the size of the whole PsdImage and render with a saving layer position.
To get this result try to replace this section of your code:

        // Write this layer to disk
        objLayer.save(strSavedLayerName, pngOptions);

by next code example:

        // Write this layer to disk
        Rectangle savingBounds = new Rectangle(-objLayer.getLeft(), -objLayer.getTop(), psdImage.getWidth(), psdImage.getHeight());
        objLayer.save(strSavedLayerName, pngOptions, savingBounds);