Skip to main content

Create, Load, and Save PowerPoint Presentations

  • 4 minutes to read

The DevExpress Presentation API allows you to:

  • Create new PowerPoint presentations
  • Load presentations from streams or byte arrays
  • Save presentations in different PowerPoint formats

Supported presentation formats:

  • PPTX
  • PPTM
  • POTX
  • POTM

Create a Presentation

Use the parameterless Presentation() constructor to create a new PPTX presentation.

import com.devexpress.docs.presentation.*;

// Create a presentation.
try(Presentation presentation = new Presentation()) {

    // The presentation contains an empty slide by default.
    Slide slide = presentation.getSlides().getFirst();

    // Your implementation goes here.
}

Load a Presentation from a Stream

Tip

Configure security loading limits and options before you load a presentation to protect your application from malicious or malformed files.

Refer to the following help topic for additional information: Secure Presentation Processing.

Use the Presentation(InputStream) constructor to load a presentation from a stream:

import com.devexpress.docs.presentation.*;
import java.io.FileInputStream;

try(
    // Open a stream to an existing presentation file.
    FileInputStream inputStream = new FileInputStream("my-presentation.pptx");

    // Load the presentation from the input stream.
    Presentation presentation = new Presentation(inputStream)
) {
    // Your implementation goes here.
}

If you know the document format, specify it explicitly to improve load performance.

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

// Load a PPTM presentation from a file into memory.
try(Presentation presentation =
                new Presentation(Files.readAllBytes(
                        Path.of("my-presentation.pptm")), DocumentFormat.PPTM)
) {
    // Your implementation goes here.
}

The following code snippet loads a presentation from a ReadableByteChannel:

import com.devexpress.docs.presentation.DocumentFormat;
import com.devexpress.docs.presentation.Presentation;

import java.nio.channels.*;
import java.nio.file.*;

// Open a readable byte channel to an existing presentation file.
try (ReadableByteChannel channel =
             FileChannel.open(Path.of("my-presentation.pptx"),
                     StandardOpenOption.READ);
     Presentation presentation =
             new Presentation(channel, DocumentFormat.PPTX)) {

    // Work with the presentation here.
}

Warning

If you try to load an empty or corrupted presentation file, or a file in an unsupported format, the API throws a JavaException. The exception message is “Cannot load the document.”

Load a Presentation from a Byte Array

Use the Presentation(Byte[]) or Presentation(Byte[], DocumentFormat) constructor to load a presentation from a byte array:

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

// Load a PPTX presentation from a file into memory.
// Document format is explicitly specified as PPTX to improve load performance.
try (Presentation presentation =
             new Presentation(Files.readAllBytes(
                     Path.of("my-presentation.pptx")), DocumentFormat.PPTX)) {

        // Your implementation goes here.
}

Warning

If you try to load an empty or corrupted presentation file, or a file in an unsupported format, the API throws a JavaException. The exception message is “Cannot load the document.”

Save the Presentation

Tip

Sanitize a presentation before you save it if the output file may contain private, hidden, or other sensitive content.

Refer to the following help topic for additional information: Sanitize Presentation Content

Call a Presentation.save method overload to save the presentation to a stream, a byte channel, or a byte array (for example, Presentation.save(stream) or Presentation.save()).

The default output format is PPTX. To specify another format, call a save method overload with the documentFormat parameter (for example, Presentation.save(stream, documentFormat)).

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

try (Presentation presentation = new Presentation()) {
    // Ensure the output directory path is resolved.
    Path outputDir = Path.of("output");
    Files.createDirectories(outputDir);

    try (FileOutputStream fileStream = new FileOutputStream(
            outputDir.resolve("my-presentation-copy.pptm").toFile())) {

        // Save the presentation to a PPTM file.
        presentation.save(fileStream, DocumentFormat.PPTM);
    }
}

The following code snippet loads a presentation from a file and saves it to a file channel in PPTX format:

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

public class Main {
    public static void main(String[] args) throws Exception {
        // Load an existing presentation.
        try (Presentation presentation = new Presentation(Files.readAllBytes(Path.of("Document.pptx")))) {
            // Save the presentation to a new file.
            Files.createDirectories(Path.of("output"));
            try (WritableByteChannel channel = FileChannel.open(Path.of("output", "Document.pptx"),
                    StandardOpenOption.CREATE,
                    StandardOpenOption.WRITE,
                    StandardOpenOption.TRUNCATE_EXISTING)) {
                presentation.save(channel, DocumentFormat.PPTX);
            }
        }
    }
}

Next Steps

Configure Slide Masters and Layouts
Learn how to configure slide masters and layouts.
Manage Slides
Learn how to create and customize slides in a presentation.