Psd-java The saved psd file cannot be opened

After deleting the layers and introducing a new smartobject and saving the file, I cannot open the saved PSD file using Photoshop.
// 加载 PSD 文件
String materialPath = “ZNDX.psd”;
PsdImage materImage = (PsdImage) Image.load(materialPath);

        // 新智能对象文件路径
        String filePath = "TRF6242.png";
        FileStream stream = new FileStream(filePath, FileMode.Open);

        // 创建新的智能对象图层
        SmartObjectLayer smartObjectLayer = new SmartObjectLayer(stream);

        // 获取现有图层并转换为列表
        List<Layer> layers = new ArrayList<>(Arrays.asList(materImage.getLayers()));

        // 删除索引 0 的图层
        if (!layers.isEmpty()) {
            Layer removedLayer = layers.remove(0);
            System.out.println("已删除图层: " + removedLayer.getName());
        }

        // 将新的智能对象图层添加到图层列表
        layers.add(0, smartObjectLayer); // 添加到图层的第一个位置
        System.out.println("已添加新的智能对象图层!");

        // 更新图层数组到 PSD 文件
        materImage.setLayers(layers.toArray(new Layer[0]));

        // 保存修改后的 PSD 文件
        String outputPsdPath = "output.psd";
        materImage.save(outputPsdPath);
        materImage.dispose();

@houdajin

It seems that you are encountering an issue with opening a PSD file in Photoshop after modifying it using Aspose.PSD for Java. This can happen for several reasons, especially when dealing with smart object layers and their contents.

Here are a few suggestions to troubleshoot and potentially resolve the issue:

  1. Check Smart Object Layer Handling: Ensure that the smart object layer is being handled correctly. When you create a new smart object layer, make sure that the contents are properly linked or embedded. If the smart object is linked to an external file, ensure that the path is correct and that the file exists.

  2. Update Smart Object Contents: After modifying the smart object layer, you should call the method to update the modified content. This ensures that any changes made to the smart object are reflected in the PSD file. For example, you can use the UpdateModifiedContent() method on the smart object layer after making changes.

  3. Save Options: When saving the modified PSD file, ensure that you are using the correct options. The PsdOptions class can be used to specify how the PSD file should be saved. Make sure to pass the current image instance to it.

  4. Layer Management: When you remove and add layers, ensure that the layer order and properties are correctly set. Sometimes, if layers are not properly managed, it can lead to issues when opening the file in Photoshop.

  5. Testing with Sample Files: If possible, try to replicate the issue with a simpler PSD file. This can help determine if the problem is with the specific file you are using or with the code itself.

Here is a sample code snippet that demonstrates how to properly handle smart object layers:

// Load the PSD file
String materialPath = "ZNDX.psd";
PsdImage materImage = (PsdImage) Image.load(materialPath);

// Create a new smart object layer
String filePath = "TRF6242.png";
FileStream stream = new FileStream(filePath, FileMode.Open);
SmartObjectLayer smartObjectLayer = new SmartObjectLayer(stream);

// Remove the first layer and add the new smart object layer
List<Layer> layers = new ArrayList<>(Arrays.asList(materImage.getLayers()));
if (!layers.isEmpty()) {
    layers.remove(0); // Remove the first layer
}
layers.add(0, smartObjectLayer); // Add the new smart object layer

// Update the layers in the PSD file
materImage.setLayers(layers.toArray(new Layer[0]));

// Update the smart object contents if necessary
smartObjectLayer.UpdateModifiedContent();

// Save the modified PSD file
String outputPsdPath = "output.psd";
materImage.save(outputPsdPath, new PsdOptions(materImage));
materImage.dispose();

By following these steps and ensuring that the smart object layers are correctly managed, you should be able to save a PSD file that opens successfully in Photoshop. If the issue persists, consider checking the Aspose.PSD documentation or reaching out to the support forum for further assistance.

Sources:
[1]: Aspose.PSD for Java - Aspose Documentation

@houdajin could you please provide source file to help Aspose.PSD Team to reproduce this issue.

asposeTest.zip (8.4 MB)

This is my source file. The psd file is what I saved from photoshop. Because my overall psd file has many artboards, and there are smart objects in the artboards. The main problem is that I can’t delete the original layers in the smart object, and the psd file saved as a psd file cannot be opened in photoshop. Thank you for your help.

@houdajin
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-2300,PSDJAVA-694

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.

have another question to ask. In Photoshop, I drag an image into a smart object and need to make a transformation to ensure that the edges of the image fit perfectly in the frame. How should I operate in ASPose PSD Java?
step1.png (121.1 KB)

step2.png (149.4 KB)

Step 1 is the way I dragged the image in, and step 2 is the way I want to adjust it

@houdajin using of ReplaceContents should be enough. It replaces whole internal SmartObject and changes it width and height for the new one. But if you need to put the image to the specific place, you should create new Image and paste the needed picture fragment to the correct place and then replace content with whole image.

Here can be found examples: Smart Object Update and Export using Aspose.PSD for Java|Documentation

 // Demonstration of API to work with Smart Object Layers
    private static void smartObjectManipulationTest() {
        String source = "new_panama-papers-8-trans4.psd";
        String exportContentPath = "export_content.jpg";
        String outputOriginal = "smart_object_orig.psd";
        String outputUpdated = "smart_object.psd";

        try (PsdImage image = (PsdImage) Image.load(source)) {
            image.save(outputOriginal);
            SmartObjectLayer smartLayer = (SmartObjectLayer) image.getLayers()[0];

            // How to export content of Smart Object
            smartLayer.exportContents(exportContentPath);

            // Creating Smart Object as a Copy
            SmartObjectLayer newLayer = smartLayer.newSmartObjectViaCopy();
            newLayer.setVisible(false);
            newLayer.setDisplayName("Duplicate");

            // Get the content of Smart Object for manipulation
            try (PsdImage innerImage = (PsdImage) smartLayer.loadContents(null)) {
                invertImage(innerImage);
                smartLayer.replaceContents(innerImage);
            }

            image.getSmartObjectProvider().updateAllModifiedContent();

            PsdOptions psdOptions = new PsdOptions();
            image.save(outputUpdated, psdOptions);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }