How to Load Presentations in Java

How to Load Presentations in Java

Aspose.Slides FOSS for Java lets you open any .pptx file, inspect its content, and either save it back to PPTX or extract data from it. This guide covers opening a file, iterating slides, reading shape text, and round-tripping the save.

Step-by-Step Guide

Step 1: Add the Maven Dependency

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

Step 2: Open an Existing Presentation

Pass the file path to the Presentation constructor. Use try-with-resources to ensure cleanup.

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation("input.pptx")) {
    System.out.println("Slide count: " + prs.getSlides().size());
    prs.save("output.pptx", SaveFormat.PPTX);
}

Unknown XML parts in the source file are preserved verbatim: the library never removes content it does not yet understand.


Step 3: Inspect Slides

Iterate over all slides and print their shape count:

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation("deck.pptx")) {
    for (int i = 0; i < prs.getSlides().size(); i++) {
        ISlide slide = prs.getSlides().get(i);
        int shapeCount = slide.getShapes().size();
        System.out.println("Slide " + i + ": " + shapeCount + " shapes");
    }
}

Step 4: Read Shape Text

Iterate over shapes and read text from shapes that have a TextFrame:

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation("deck.pptx")) {
    for (ISlide slide : prs.getSlides()) {
        for (IShape shape : slide.getShapes()) {
            if (shape instanceof IAutoShape) {
                IAutoShape autoShape = (IAutoShape) shape;
                ITextFrame tf = autoShape.getTextFrame();
                if (tf != null && !tf.getText().isEmpty()) {
                    System.out.println("  Shape text: " + tf.getText());
                }
            }
        }
    }
}

Step 5: Read Document Properties

Access core document properties from prs.getDocumentProperties():

import org.aspose.slides.foss.*;

try (Presentation prs = new Presentation("deck.pptx")) {
    IDocumentProperties props = prs.getDocumentProperties();
    System.out.println("Title:   " + props.getTitle());
    System.out.println("Author:  " + props.getAuthor());
    System.out.println("Subject: " + props.getSubject());
}

Step 6: Round-Trip Save

After inspecting or modifying the presentation, save it back to PPTX:

prs.save("output.pptx", SaveFormat.PPTX);

Saving to a different path creates a new file. Saving to the same path overwrites the original.


Common Issues and Fixes

java.io.FileNotFoundException

Check that the path to the .pptx file is correct relative to the working directory. Use java.nio.file.Paths for robust path construction:

import java.nio.file.Path;
import java.nio.file.Paths;

Path path = Paths.get("assets", "deck.pptx");
try (Presentation prs = new Presentation(path.toString())) {
    // ...
}

Exception: File format is not supported

The library supports .pptx (Office Open XML) only. Legacy .ppt (binary PowerPoint 97-2003) files are not supported.

Shapes have no getTextFrame() method

Some shapes (Connectors, PictureFrames, GroupShapes) do not have a text frame. Check with instanceof IAutoShape before casting and accessing text.


Frequently Asked Questions

Does loading preserve all original content?

Yes. Unknown XML parts are preserved verbatim on round-trip save. The library only serializes the parts of the document model it understands, and passes through any XML it does not recognize.

Can I load a password-protected PPTX?

Password-protected (encrypted) presentations are not supported in this edition.

Is loading from an InputStream supported?

Yes. The Presentation constructor accepts an InputStream:

import java.io.FileInputStream;

try (FileInputStream fis = new FileInputStream("deck.pptx");
     Presentation prs = new Presentation(fis)) {
    System.out.println("Slides: " + prs.getSlides().size());
}

See Also