1.源代码:
public static void main(String[] args) throws FileNotFoundException { License license = new License(); String licFileDir = "E:\\mysite\\openproj\\Aspose.PSD-for-Java\\Examples\\src\\main\\resources\\Lic\\Aspose.Total.Product.Family.lic"; license.setLicense(licFileDir); String dataDir = "E:\\mysite\\openproj\\Aspose.PSD-for-Java\\Examples\\src\\main\\resources\\PSD\\"; String sourceFileName = dataDir + "nvwangJy.psd"; String exportPath = dataDir + "nvwangNewTextUpdateTwoResult.png"; PsdLoadOptions loadOptions = new PsdLoadOptions(); //Try this //loadOptions.setLoadEffectsResource(true); PsdImage image =(PsdImage) Image.load(sourceFileName,loadOptions); for(int i=0; i < image.getLayers().length; i++ ) { Layer layer = (Layer) image.getLayers()[i]; //System.out.println(i + "DisplayName:" + layer.getDisplayName() + "name:" + layer.getName()); if (i == 11 && image.getLayers()[i] instanceof TextLayer) { TextLayer textLayer = (TextLayer)image.getLayers()[i]; System.out.println("this is a textLayer"); byte[] layerBytes; MemoryStream layerMem = new MemoryStream(); OutputStream dstStream = layerMem.toOutputStream(); textLayer.save(dstStream, new PsdOptions()); layerBytes = layerMem.toArray(); layerMem = new MemoryStream(layerBytes); //InputStream inputStream = new ByteArrayInputStream(layerBytes); //textLayer = new Layer(layerMem); textLayer = (TextLayer)new Layer(layerMem.toInputStream()); Point leftTopCoordinate = new Point(); leftTopCoordinate.setX(textLayer.getLeft()); leftTopCoordinate.setY(textLayer.getTop()); textLayer.updateText("靠你就是我\r我的\r你的", leftTopCoordinate, ((TextLayer)textLayer).getFont().getSize(), ((TextLayer)textLayer).getTextColor()); } } PngOptions options = new PngOptions(); options.setColorType(PngColorType.TruecolorWithAlpha); image.save(exportPath,options); }
2.源psd文件:
nvwangJy.zip (2.6 MB)
3.实际结果:
image.png (8.6 KB)
4.期望结果:
nvwangNewTextUpdateExpect.jpg (277.6 KB)
PS:希望官方有支持TextLayer、Layer、PsdImage->byte[]->反序列化源对象(源TextLayer、Layer、PsdImage)。和达到LoadPsd源文件的图层操作效果。说白了,Image.load(sourceFileName,loadOptions)出来的图层结果和图层layerBytes[]转换出来的Layer图层一致。即底层对象序列化,反序列化结果内存地址一致、属同一引用对象。