Working with Images in Presentations — Aspose.Slides FOSS for Java

Aspose.Slides FOSS for Java lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame shapes. Images can also be used as shape background fills via FillType.PICTURE.


Adding an Image from File

Load image bytes from disk and add them to the presentation’s image collection with prs.getImages().addImage(). Then place the image on a slide as a PictureFrame:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    // Add the image to the shared collection
    byte[] imageData = Files.readAllBytes(Path.of("logo.png"));
    var img = prs.getImages().addImage(imageData);

    // Place it on the slide as a PictureFrame
    var slide = prs.getSlides().get(0);
    slide.getShapes().addPictureFrame(ShapeType.RECTANGLE, 50, 50, 300, 200, img);

    prs.save("with-image.pptx", SaveFormat.PPTX);
}

The four positional arguments to addPictureFrame() are: x, y, width, height in points.


Adding an Image from Bytes

If you already have image bytes (e.g., downloaded from a URL or read from a database), pass them directly to addImage():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

// Simulate having bytes in memory
byte[] imageBytes = Files.readAllBytes(Path.of("photo.jpg"));

try (Presentation prs = new Presentation()) {
    var img = prs.getImages().addImage(imageBytes);
    prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 100, 80, 400, 250, img);
    prs.save("from-bytes.pptx", SaveFormat.PPTX);
}

Positioning and Sizing a PictureFrame

The PictureFrame returned by addPictureFrame() inherits all Shape geometry properties and can be repositioned after creation:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] imageData = Files.readAllBytes(Path.of("photo.jpg"));
    var img = prs.getImages().addImage(imageData);

    var pf = prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 0, 0, 100, 100, img);

    // Reposition and resize after creation
    pf.setX(50);
    pf.setY(100);
    pf.setWidth(350);
    pf.setHeight(250);

    prs.save("positioned.pptx", SaveFormat.PPTX);
}

Using an Image as a Shape Fill

Any shape (not just PictureFrame) can use an image as its background fill. Set FillType.PICTURE and assign the image to getPictureFillFormat().getPicture().setImage():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.FillType;
import org.aspose.slides.foss.PictureFillMode;
import org.aspose.slides.foss.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] imageData = Files.readAllBytes(Path.of("background.png"));
    var img = prs.getImages().addImage(imageData);

    var slide = prs.getSlides().get(0);
    var shape = slide.getShapes().addAutoShape(ShapeType.ROUND_CORNER_RECTANGLE, 50, 50, 400, 250);
    shape.getFillFormat().setFillType(FillType.PICTURE);
    shape.getFillFormat().getPictureFillFormat().setPictureFillMode(PictureFillMode.STRETCH);
    shape.getFillFormat().getPictureFillFormat().getPicture().setImage(img);

    prs.save("picture-fill.pptx", SaveFormat.PPTX);
}

PictureFillMode.STRETCH scales the image to fill the entire shape. Use TILE for repeating patterns.


Adding Multiple Images Across Slides

Images added to prs.getImages() are shared across all slides. The same IPPImage object can be used on multiple slides without duplicating the data:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.SaveFormat;
import java.nio.file.Files;
import java.nio.file.Path;

try (Presentation prs = new Presentation()) {
    byte[] logoData = Files.readAllBytes(Path.of("logo.png"));
    var logo = prs.getImages().addImage(logoData);

    // Add the same image to the first slide
    prs.getSlides().get(0).getShapes().addPictureFrame(ShapeType.RECTANGLE, 600, 10, 100, 40, logo);

    prs.save("shared-image.pptx", SaveFormat.PPTX);
}

See Also