Skip to main content

Manage Slides — Presentation API for Java

  • 4 minutes to read

The DevExpress Presentation API allows you to create, modify, duplicate, reorder, and remove slides. You can also create slides based on slide master layouts and copy slides between presentations.

Access Presentation Slides

Use the Presentation.getSlides() method to access slides in the presentation.

import com.devexpress.docs.presentation.*;

// Access presentation slides.
ISlideCollection slides = presentation.getSlides();

// Access the first slide in the presentation.
Slide firstSlide = presentation.getSlides().getFirst();

Add a Slide

To add a slide to a presentation:

  1. Create a Slide object. Specify a layout type in the constructor.
  2. Add the slide to the presentation.
import com.devexpress.docs.presentation.*;

// Create a presentation.
try(Presentation presentation = new Presentation()) {
    // Create a blank slide.
    Slide slide = new Slide(SlideLayoutType.BLANK, "Slide_Name");

    // Add the slide to the presentation.
    presentation.getSlides().add(slide);

    // Your additional implementation goes here.
}

Create a Slide Based on a Slide Master Layout

A Slide Master defines common presentation content and formatting (backgrounds, theme, text styles, and placeholder layouts).

When you create a slide based on a slide master layout, the slide inherits formatting and placeholder structure from the selected layout.

The following code snippet obtains a title slide layout from the first slide master and creates a new slide based on this layout:

import com.devexpress.docs.presentation.*;

// Get the first slide master's title layout.
// Create the layout if it does not exist.
SlideLayout layout =
    presentation.getSlideMasters()
                .get(0)
                .getLayouts()
                .getOrCreate(SlideLayoutType.TITLE);

// Create a slide based on the layout.
Slide slide = new Slide(layout);

// Add the slide to the presentation.
presentation.getSlides().add(slide);

Refer to the following help topic for additional information: Configure Slide Masters and Layouts.

Iterate Through Slides in a Presentation

Use standard iteration constructs such as for-each loops to traverse slides in the presentation.

import com.devexpress.docs.presentation.*;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws Exception {
        Path path = Path.of("presentation.pptx");
        try (Presentation presentation =
                     new Presentation(Files.readAllBytes(path))) {

            int index = 0;

            // Iterate through all slides in the presentation.
            for (Slide slide : presentation.getSlides()) {

                // Access slide content (shapes).
                System.out.println("Slide " + index +
                        " contains " + slide.getShapes().size() + " shapes.");

                index++;
            }
        }
    }
}

Manage Slide Visibility

Use the Slide.setVisible(boolean) method to display or hide a slide.

The following code snippet removes all hidden slides from the presentation:

import com.devexpress.docs.presentation.*;

// Iterate through slides in reverse order and remove hidden slides.
for (int i = presentation.getSlides().size() - 1; i >= 0; i--) {
    Slide slide = presentation.getSlides().get(i);

    if (!slide.getVisible()) {
        presentation.getSlides().remove(i);
    }
}

Duplicate Slides

Use the Slide.deepClone() method to create a copy of the slide and all its content:

// Create a copy of the slide.
Slide clonedSlide = slide.deepClone();

// Add the copy to the presentation.
presentation.getSlides().add(clonedSlide);

Reorder Slides

Use the move() method to change the position of slides within the collection:

// Move the specified slide to index 1.
presentation.getSlides().move(slide, 1);

// Move the slide from index 3 to index 0.
presentation.getSlides().move(3, 0);

Copy Slides from Another Presentation

You can merge presentations by copying slides from one presentation into another.

The following code snippet implements the mergePresentations method that merges an array of presentations into a single presentation.

// Merges an array of presentations into a single presentation.
private static Presentation mergePresentations(Presentation[] presentations) {
    // Create an empty presentation (a merge target).
    Presentation mergedPresentation = new Presentation();

    // Remove default slide created by the constructor.
    mergedPresentation.getSlides().clear();

    // Iterate through all source presentations.
    for(Presentation sourcePresentation : presentations) {

        // Append all slides from the current presentation.
        for(Slide slide : sourcePresentation.getSlides()) {
            mergedPresentation.getSlides().add(slide);
        }
    }

    return mergedPresentation;
}

Refer to the following help topic for additional information: Merge and Split Presentations.

Remove Slides

Use the remove() and clear() methods to delete one slide, a slide at a specific index, or the entire slide collection.

// Removes the specified slide.
presentation.getSlides().remove(slide);

// Remove the second slide.
presentation.getSlides().remove(1);

// Remove all slides.
presentation.getSlides().clear();
See Also