PsdImage export to PNG error

I put all the sublayers in the artboard into a new PsdImage, and I want to export it as a PNG image, but currently only the first artboard is exported as a PNG image normally, and the rest of the content is empty
right.jpg (68.9 KB)

2_custom_canvas.png (186 字节)

// 加载 PSD 文件
String psdPath = “1 DTD彩银.psd”;
PsdImage psdImage = (PsdImage) com.aspose.psd.Image.load(psdPath);
for (Layer layer : psdImage.getLayers()) {
if (layer instanceof LayerGroup) { // 检查是否为画板(图层组)
LayerGroup layerGroup = (LayerGroup) layer;
System.out.println(“画板名称”+layerGroup.getName());
// 获取所有子图层
Layer[] childLayers = layerGroup.getLayers();
for (Layer childLayer : childLayers) {
System.out.println(childLayer.getName());
}
PsdImage newCanvas = new PsdImage(800, 800);
newCanvas.setLayers(childLayers);

            // 保存合并后的结果为 PSD
            String mergedPngPath = layerGroup.getName() + "_custom_canvas.png";
            String mergedPsdPath = layerGroup.getName() + "_custom_canvas.psd";
            PngOptions pngOptions = new PngOptions();
            pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
            newCanvas.save(mergedPngPath, pngOptions);
            newCanvas.save(mergedPsdPath, new PdfOptions());
            newCanvas.dispose();
            System.out.println("画板 \"" + layerGroup.getName() + "\" 已合并并保存为 PSD:" + mergedPsdPath);
            System.out.println("完成");

@houdajin

It seems that you are encountering an issue where only the first artboard is being exported as a PNG image, while the rest of the content is empty. This can happen if the layers are not being properly managed or if the new canvas is not correctly set up to include all the necessary layers.

To ensure that all layers from each artboard are exported correctly, you can follow these steps:

  1. Load the PSD File: Make sure you are loading the PSD file correctly and that it contains the expected layers.

  2. Iterate Through Layer Groups: Instead of creating a new canvas for each layer group, you should ensure that you are correctly adding all child layers to the new canvas.

  3. Export Each Layer Group: When exporting, ensure that you are saving each layer group to a separate PNG file.

Here’s a modified version of your code that demonstrates how to export each layer group correctly:

import com.aspose.psd.Image;
import com.aspose.psd.fileformats.png.PngColorType;
import com.aspose.psd.fileformats.psd.PsdImage;
import com.aspose.psd.fileformats.psd.layers.Layer;
import com.aspose.psd.fileformats.psd.layers.LayerGroup;
import com.aspose.psd.imageoptions.PngOptions;

public class ExportLayerGroups {
    public static void main(String[] args) {
        String psdPath = "1 DTD彩银.psd";
        PsdImage psdImage = (PsdImage) Image.load(psdPath);
        PngOptions pngOptions = new PngOptions();
        pngOptions.setColorType(PngColorType.TruecolorWithAlpha);

        for (Layer layer : psdImage.getLayers()) {
            if (layer instanceof LayerGroup) {
                LayerGroup layerGroup = (LayerGroup) layer;
                System.out.println("Exporting layer group: " + layerGroup.getName());

                // Create a new canvas for the layer group
                PsdImage newCanvas = new PsdImage(800, 800);
                newCanvas.setLayers(layerGroup.getLayers());

                // Save the layer group as PNG
                String mergedPngPath = layerGroup.getName() + "_custom_canvas.png";
                newCanvas.save(mergedPngPath, pngOptions);
                newCanvas.dispose();
                System.out.println("Layer group \"" + layerGroup.getName() + "\" exported as PNG: " + mergedPngPath);
            }
        }
    }
}

In this code:

  • Each layer group is processed, and a new canvas is created for it.
  • The child layers of the layer group are set to the new canvas.
  • Finally, the canvas is saved as a PNG file.

Make sure that the dimensions of the new canvas are appropriate for the content you are exporting. Adjust the size as necessary to fit the layers properly.

If you continue to experience issues, please check the structure of your PSD file to ensure that all layers are correctly defined and accessible.

For more detailed guidance on exporting layers from PSD files, you can refer to the official documentation on exporting PSD layers to raster images using Java.

Sources:
[1]: Export PSD Layers to Raster Images using Java | Aspose.PSD Java API