How to Add Images to PowerPoint Slides in Java
This guide shows how to add images to PowerPoint slides using Aspose.Slides FOSS for Java. Images are embedded as picture frames by calling slide.getShapes().addPictureFrame(). The image data is stored once in the prs.getImages() collection and referenced by the frame.
Prerequisites
Add the following dependency to your pom.xml to include the Aspose.Slides FOSS library:
<dependency>
<groupId>org.aspose.slides.foss</groupId>
<artifactId>aspose-slides-foss</artifactId>
<version>1.0.0</version>
</dependency>Add an Image from a File
Read the image bytes with Files.readAllBytes(), add them to the presentation’s image collection with prs.getImages().addImage(), then attach them to a slide with addPictureFrame():
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.IPPImage;
import org.aspose.slides.foss.IPictureFrame;
import org.aspose.slides.foss.ILayoutSlide;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Paths;
try (Presentation prs = new Presentation()) {
ISlideCollection slides = prs.getSlides();
ISlide slide = slides.get(0);
// Load image into the presentation's image collection
byte[] imageData = Files.readAllBytes(Paths.get("photo.jpg"));
IImageCollection images = prs.getImages();
IPPImage img = images.addImage(imageData);
// Add a picture frame at (x=50, y=50, width=400, height=300) in points
IPictureFrame frame = slide.getShapes().addPictureFrame(
ShapeType.RECTANGLE,
50, 50, 400, 300,
img
);
prs.save("with-image.pptx", SaveFormat.PPTX);
}The addPictureFrame() method accepts the shape type, position (x, y), dimensions (width, height in points), and the IPPImage reference. Its full signature:
addPictureFrame(shapeType, x, y, width, height, image) -> IPictureFrameAll dimensions are in points (1 point = 1/72 inch). For a standard 10 x 7.5 inch slide the coordinate space is 720 x 540 points.
Add an Image from a Stream
If you already have the image as an InputStream (e.g., downloaded from a URL or read from a database):
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.IPPImage;
import org.aspose.slides.foss.IPictureFrame;
import org.aspose.slides.foss.ILayoutSlide;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.io.FileInputStream;
try (Presentation prs = new Presentation();
FileInputStream fis = new FileInputStream("logo.png")) {
IImageCollection images = prs.getImages();
IPPImage img = images.addImage(fis);
prs.getSlides().get(0).getShapes().addPictureFrame(
ShapeType.RECTANGLE,
200, 100, 300, 200,
img
);
prs.save("logo-slide.pptx", SaveFormat.PPTX);
}Add Multiple Images to Different Slides
Create one slide per image by calling prs.getSlides().addEmptySlide(), then embed each image on its corresponding slide using addPictureFrame():
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.IPPImage;
import org.aspose.slides.foss.IPictureFrame;
import org.aspose.slides.foss.ILayoutSlide;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Paths;
String[] imageFiles = {"slide1.jpg", "slide2.jpg", "slide3.jpg"};
try (Presentation prs = new Presentation()) {
ISlideCollection slides = prs.getSlides();
ILayoutSlide layout = slides.get(0).getLayoutSlide();
// Ensure enough slides exist
while (prs.getSlides().size() < imageFiles.length) {
prs.getSlides().addEmptySlide(layout);
}
for (int i = 0; i < imageFiles.length; i++) {
if (!Files.exists(Paths.get(imageFiles[i]))) continue;
byte[] data = Files.readAllBytes(Paths.get(imageFiles[i]));
IImageCollection images = prs.getImages();
IPPImage img = images.addImage(data);
prs.getSlides().get(i).getShapes().addPictureFrame(
ShapeType.RECTANGLE, 0, 0, 720, 540, img
);
}
prs.save("multi-image.pptx", SaveFormat.PPTX);
}Count Images in an Existing Presentation
Load an existing presentation and use prs.getImages().size() to count the embedded images:
import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.IPPImage;
import org.aspose.slides.foss.IPictureFrame;
import org.aspose.slides.foss.ILayoutSlide;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;
try (Presentation prs = new Presentation("with-image.pptx")) {
System.out.println("Presentation contains "
+ prs.getImages().size() + " image(s)");
}The prs.getImages() collection is shared across all slides: the same image bytes are stored once even if the picture frame appears on multiple slides.