Replacing Images inside Presentation Image Collection

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:

  1. Load the presentation file with image inside it using the Presentation class.
  2. Load an image from file in byte array.
  3. Replace the target image with new image in byte array
  4. In second approach load the image in Image object and replace the target image with loaded image.
  5. In third approach replace the image with already added image in presentation image collection.
  6. Write the modified presentation as a PPTX file.
try {
//Instantiate the presentation
Presentation presentation = new Presentation("presentation.pptx");
//The first method
File file = new File("image1.png");
byte[]data = new byte[(int) file.length()];
InputStream inputStream = null;
try
{
inputStream = new FileInputStream(file);
inputStream.read(data);
}
finally
{
inputStream.close();
}
IPPImage oldImage = presentation.getImages().get_Item(0);
oldImage.replaceImage(data);
//The second method
BufferedImage newImage = ImageIO.read(new File("image0.jpeg"));
IPPImage imageToReplace= presentation.getImages().addImage(newImage);
oldImage = presentation.getImages().get_Item(1);
oldImage.replaceImage(imageToReplace);
//The third method
oldImage = presentation.getImages().get_Item(2);
oldImage.replaceImage(presentation.getImages().get_Item(3));
//Save the presentation
presentation.save("presentation_out.pptx", SaveFormat.Pptx);
} catch (Exception e) {
}