Hello,
We are seeing an issue where an image that is replaced one Shape can affect other Shapes in the Presentation. This happens when the same image is used by multiple Shapes. When we replace the image in one Shape, it affects used of all other Shapes.
We understand that even in Microsoft PowerPoint, when you use the same image in two or more Shapes, the same image file is recycled within the PPTX. So when “Shape 1” and “Shape 2” have the same image, the PPTX will be saved with only 1 file in the “media” folder. However, when the image is replaced in one of the Shapes (which uses the “shared image”), all other Shapes that share the image are also updated with the new image. Our expectation is that when the image is replaced in one Shape, it applies to that Shape only and does not affect any other Shape.
This behavior can be seen in the latest Aspose Slides for Java version 20.9, the attached PNG files and the following Java code:
final String pptFile = [PATH] + "ReplaceImageIssue.pptx";
final String imgFile1 = [PATH] + "chart1.png";
final String imgFile2 = [PATH] + "chart2.png";
IPresentation ppt = new Presentation();
// load the image bytes
final byte[] imgBytes = Files.readAllBytes(Paths.get(imgFile1));
// Add 4 new empty Slides
ILayoutSlide layout = ppt.getMasters().get_Item(0).getLayoutSlides()
.getByType(SlideLayoutType.Blank);
for (int i = 0; i < 4; i++) {
ISlide slide = ppt.getSlides().insertEmptySlide(0, layout);
// add the image to the new slide
IPPImage img = ppt.getImages().addImage(imgBytes);
slide.getShapes().addPictureFrame(ShapeType.Rectangle, 10, 20,
img.getWidth(), img.getHeight(), img);
}
// Save the new PPTX
Files.deleteIfExists(Paths.get(pptFile));
ppt.save(pptFile, SaveFormat.Pptx);
System.out.println("Saved ORIGINAL PPTX file: " + pptFile);
// Now reload the PPTX and replace the image in Slide #3 (only)
ppt = new Presentation(pptFile);
// Get the only Shape that exists in Slide #3
IShape shape = ppt.getSlides().get_Item(2).getShapes().get_Item(0);
IPPImage existingImg = ((IPictureFrame)shape).getPictureFormat().getPicture().getImage();
// Replace the existing image with Chart2
final byte[] newImgBytes = Files.readAllBytes(Paths.get(imgFile2));
existingImg.replaceImage(newImgBytes);
// Save the modified PPTX
final String newPptx = pptFile.replace(".pptx", "_mod.pptx");
Files.deleteIfExists(Paths.get(newPptx));
ppt.save(newPptx, SaveFormat.Pptx);
System.out.println("Saved MODIFIED PPTX file: " + newPptx);
Running the above code should produce:
- A new PPTX file similar to the attached
ReplaceImageIssue.pptx
file. If you open the PPTX in PowerPoint, you should see there are 4 Slides and each has one Shape displaying the same image - derived from the “Chart1.png” file. - A new PPTX file similar to the attached
ReplaceImageIssue_mod.pptx
file. If you open the PPTX in PowerPoint, you should see all 4 Shapes are displaying the same new image - derived from the “Chart2.png” file. However, the code replaced the Shape from Slide #3 only. The other Shapes should not have been modified.
Environment Details:
- Aspose Slides for Java 20.9
- Java version 1.8.0_211
- Windows 10 OS (but also reproducible under Linux).
File description in the ReplaceImage.zip
(70.9 KB) attachment contains:
- chart1.png: PNG file that is initially used as the “shared image” on the 4 Shapes added by the code above.
- chart2.png: PNG file that is to replace the image for the Shape in Slide #3 per the code above.
- ReplaceImageIssue.pptx: PPTX file produced from the code above on our environment and shows “chart1.png” as the shared image.
- ReplaceImageIssue_mod.pptx: PPTX file produced from the code above on our environment after the image from Slide #3 is replaced.
Thank you!