How to Add Images to PowerPoint Slides in Java

How to Add Images to PowerPoint Slides in Java

Images in Aspose.Slides FOSS are embedded as picture frames, shapes that hold an image and can be positioned, resized, and styled like any other shape. The image data is stored once in the prs.getImages() collection and referenced by the frame.


Prerequisites

<dependency>
  <groupId>org.aspose.slides.foss</groupId>
  <artifactId>aspose-slides-foss</artifactId>
  <version>1.0.0</version>
</dependency>

Add an Image from a File

import org.aspose.slides.foss.*;
import java.nio.file.Files;
import java.nio.file.Paths;

try (Presentation prs = new Presentation()) {
    ISlide slide = prs.getSlides().get(0);

    // Load image into the presentation's image collection
    byte[] imageData = Files.readAllBytes(Paths.get("photo.jpg"));
    IPPImage img = prs.getImages().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 signature:

addPictureFrame(shapeType, x, y, width, height, image) -> IPictureFrame

All 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.*;
import java.io.FileInputStream;

try (Presentation prs = new Presentation();
     FileInputStream fis = new FileInputStream("logo.png")) {
    IPPImage img = prs.getImages().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

import org.aspose.slides.foss.*;
import java.nio.file.Files;
import java.nio.file.Paths;

String[] imageFiles = {"slide1.jpg", "slide2.jpg", "slide3.jpg"};

try (Presentation prs = new Presentation()) {
    ILayoutSlide layout = prs.getSlides().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]));
        IPPImage img = prs.getImages().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

import org.aspose.slides.foss.*;

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.


See Also