I would like to ask you for advice. I have many artboards in my psd, and I need to export the artboards as PNG images separately. However, the PNG images exported by my current code are wrong.
right.jpg (68.9 KB)
right2.jpg (71.0 KB)
// 加载 PSD 文件
String psdPath = "1 DTD彩银.psd";
PsdImage psdImage = (PsdImage) com.aspose.psd.Image.load(psdPath);
for (Layer layer1 : psdImage.getLayers()) {
if (layer1 instanceof LayerGroup) { // 检查是否为画板(图层组)
LayerGroup artboard = (LayerGroup) layer1;
// 隐藏所有其他画板
for (Layer otherLayer : psdImage.getLayers()) {
otherLayer.setVisible(otherLayer == artboard);
}
String name = artboard.getName();
PngOptions pngSaveOpt = new PngOptions();
pngSaveOpt.setColorType(PngColorType.TruecolorWithAlpha);
artboard.save(name+".png",pngSaveOpt);
System.out.println(name+"保存成功");
}
}
wrong.jpg (23.2 KB)
wrong2.png (1020.0 KB)
@houdajin
To export artboards from a PSD file as separate PNG images using Aspose.PSD for Java, you can follow the steps outlined below. It seems that your current implementation may not be correctly saving the artboards, so I will provide a refined approach.
Step-by-Step Guide to Export Artboards as PNG
-
Load the PSD File: Start by loading your PSD file into a PsdImage
object.
-
Loop Through Layers: Iterate through the layers of the PSD file to identify the artboards (which are typically represented as LayerGroup
).
-
Set Visibility: For each artboard, set it to be visible while hiding all other layers.
-
Configure PNG Options: Set the options for exporting the images, ensuring you maintain the desired quality and transparency.
-
Save Each Artboard: Save each artboard as a PNG file.
Here’s a sample code snippet that demonstrates this process:
import com.aspose.psd.Image;
import com.aspose.psd.fileformats.png.PngColorType;
import com.aspose.psd.fileformats.psd.PsdImage;
import com.aspose.psd.imageoptions.PngOptions;
import com.aspose.psd.layers.Layer;
import com.aspose.psd.layers.LayerGroup;
public class ExportArtboards {
public static void main(String[] args) {
// Load the PSD file
String psdPath = "1 DTD彩银.psd";
PsdImage psdImage = (PsdImage) Image.load(psdPath);
// Loop through layers to find artboards
for (Layer layer : psdImage.getLayers()) {
if (layer instanceof LayerGroup) { // Check if it's an artboard
LayerGroup artboard = (LayerGroup) layer;
// Hide all other layers
for (Layer otherLayer : psdImage.getLayers()) {
otherLayer.setVisible(otherLayer == artboard);
}
// Set PNG options
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
// Save the artboard as PNG
String name = artboard.getName();
artboard.save(name + ".png", pngOptions);
System.out.println(name + " saved successfully");
}
}
}
}
Key Points:
- Ensure that you are correctly identifying the artboards as
LayerGroup
.
- Set the visibility of layers properly to avoid saving unwanted layers.
- Use
PngOptions
to maintain the quality of the exported images.
This approach should help you export each artboard as a separate PNG file without issues. If you continue to experience problems with the exported images, please check the integrity of your PSD file and ensure that the Aspose.PSD library is correctly set up in your project.
For more detailed information on exporting PSD layers, you can refer to the official Aspose documentation on exporting PSD layers to raster images here.
@houdajin could you please provide the input file. Support of Artboard is a quite new feature for Aspose.PSD, so, we can check and improve the logic of Artboard exporting
Thank you for your reply. This is my file.[AirPortal 空投快传 复制链接到浏览器打开](AirPortal 空投快传 复制链接到浏览器打开)
@houdajin
I’ll write you back after the investigation.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PSDNET-2302
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.