Skip to main content

Merge and Split Presentations — Presentation API for Java

  • 4 minutes to read

The DevExpress Presentation API can combine slides from multiple presentations and split a presentation into smaller PowerPoint documents.

This help topic demonstrates how to:

  • Reuse slides across presentations
  • Merge multiple presentations into a single document
  • Split a presentation into multiple documents

How Presentation Merging Works

When working with merged presentations, consider the following behavior:

Slide size is defined by the target presentation
If a source slide is larger, its content may extend beyond the visible area, but remains preserved with original coordinates.
Formatting is preserved
Merged slides keep their original styles and formatting settings.
Automatic slide master transfer
When presentations are merged, slide masters from the source presentation are automatically copied to the target presentation along with their themes and layouts.

Copy a Slide from Another Presentation

The following code snippet copies a slide from one presentation and inserts it into another:

package presentation;

import com.devexpress.docs.presentation.*;
import java.io.FileOutputStream;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try(
            // Load the source presentation (slide will be copied from here).
            Presentation sourcePresentation =
                    new Presentation(Files.readAllBytes(
                            Path.of("presentation-source.pptx")));

            // Load the target presentation (slide will be inserted here).
            Presentation targetPresentation =
                    new Presentation(Files.readAllBytes(
                            Path.of("presentation-target.pptx")));
        ) {
            // Clone the first slide.
            Slide slide = sourcePresentation.getSlides()
                    .getFirst()
                    .deepClone();

            // Insert the cloned slide at the beginning of the target presentation.
            targetPresentation.getSlides().addFirst(slide);

            // Ensure output directory exists.
            Path outputDir = Path.of("output");
            Files.createDirectories(outputDir);

            // Save the updated target presentation.
            try (FileOutputStream fileStream = new FileOutputStream(
                    outputDir.resolve("presentation-target.pptx").toFile())) {

                targetPresentation.saveDocument(fileStream);
            }
        }
    }
}

Merge Presentations

The following code snippet implements the mergePresentations method that merges presentations. The method returns a new presentation that includes all slides from source presentations.

package presentation;

import com.devexpress.docs.presentation.*;
import java.io.FileOutputStream;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try(
                // Load source presentations.
                Presentation presentation1 =
                        new Presentation(Files.readAllBytes(
                                Path.of("presentation-1.pptx")));
                Presentation presentation2 =
                        new Presentation(Files.readAllBytes(
                                Path.of("presentation-2.pptx")));

                // Merge multiple presentations into a single presentation.
                Presentation mergedPresentation = mergePresentations(
                        new Presentation[]{ presentation1, presentation2 })
                ) {

            Path outputDir = Path.of("output");
            Files.createDirectories(outputDir);

            // Save the merged presentation to disk.
            try (FileOutputStream fileStream = new FileOutputStream(
                    outputDir.resolve("presentation-merged.pptx").toFile())) {
                mergedPresentation.saveDocument(fileStream);
            }
        }
    }

    // Merges an array of presentations into a single presentation.
    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;
    }
}

Split a Presentation

The following code snippet splits a presentation into two parts. It moves slides starting from the specified index into a new presentation and removes them from the original presentation:

package presentation;

import com.devexpress.docs.presentation.*;
import java.io.FileOutputStream;
import java.nio.file.*;

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

                // Split the source presentation.
                Presentation secondPresentation = splitPresentation(sourcePresentation, 2)
        ) {

            // Prepare output directory.
            Path outputDir = Path.of("output");
            Files.createDirectories(outputDir);

            // Save the first part.
            try (FileOutputStream stream =
                         new FileOutputStream(
                                 outputDir.resolve("presentation-part1.pptx").toFile())) {
                sourcePresentation.saveDocument(stream);
            }

            // Save the second part.
            try (FileOutputStream stream =
                         new FileOutputStream(
                                 outputDir.resolve("presentation-part2.pptx").toFile())) {
                secondPresentation.saveDocument(stream);
            }
        }
    }

    // Splits the specified presentation into two parts.
    // `splitIndex` - Index from which slides are moved to the new presentation.
    static Presentation splitPresentation(Presentation source, int splitIndex) {

        // Create a presentation that will contain extracted slides.
        Presentation result = new Presentation();

        // Move slides from the end until only slides [0..splitIndex-1] remain.
        for (int i = source.getSlides().size() - 1; i >= splitIndex; i--) {

            // Get a slide to move.
            Slide slide = source.getSlides().get(i);

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

            // Remove the slide from the source presentation.
            source.getSlides().remove(i);
        }

        return result;
    }
}
See Also