Replacing Images inside Presentation Image Collection
Contents
[
Hide
]
Aspose.Slides for Java makes it possible to replace images in slide shapes. This article explains how to replace an image added to the presentation image collection using different approaches.
Replacing Image inside Presentation Image Collection
Aspose.Slides for Java provides a simple API methods for replacing the images inside presentation image collection. Please follow the steps below:
- Load the presentation file with image inside it using the Presentation class.
- Load an image from file in byte array.
- Replace the target image with new image in byte array
- In second approach load the image in Image object and replace the target image with loaded image.
- In third approach replace the image with already added image in presentation image collection.
- Write the modified presentation as a PPTX file.
//Instantiate the presentation
Presentation presentation = new Presentation("presentation.pptx");
//the first way
byte[] data = Files.readAllBytes(Paths.get("image0.jpeg"));
IPPImage oldImage = presentation.getImages().get_Item(0);
oldImage.replaceImage(data);
//the second way
IImage newImage = Images.fromFile("image1.png");
oldImage = presentation.getImages().get_Item(1);
oldImage.replaceImage(newImage);
newImage.dispose();
//the third way
oldImage = presentation.getImages().get_Item(2);
oldImage.replaceImage(presentation.getImages().get_Item(3));
//Save the presentation
presentation.save("c:\\Presentations\\TestSmart.pptx", SaveFormat.Pptx);
presentation.dispose();