Edit PPT/PPTX Presentation using Java API

Presentation files come in different formats like PPT, PPTX, and ODP. You must be familiar with softwares like Microsoft PowerPoint, OpenOffice Impress, and Apple Keynote – they all work with these formats, helping us create amazing presentations. As developers, we have the power to programmatically edit these presentations in our applications. This article will guide you on how to edit PPT/PPTX presentations in Java using presentation editing API.

In this article, we’ll cover the following topics:

Java API for Presentations Editing and Automation

In our examples, we’ll be relying on a powerful GroupDocs.Editor for Java library. This library serves as a presentation editing API, enabling developers to seamlessly load, edit, and save presentations in formats such as PPT, PPTX, and PDF.

Not only does this API handle presentations, but it also supports the editing of various other document types, including word-processing documents, spreadsheets, HTML, XML, JSON, TXT, TSV, and CSV formats.

To get started, you can download the necessary JAR file from the downloads section or incorporate the latest repository and dependency Maven configurations directly into your Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-editor</artifactId>
        <version>21.3</version> 
</dependency>

Editing PPT/PPTX Presentations in Java

Once you have set up the API, you can swiftly start editing your presentation slides. Here are the steps to edit presentations in PPT/PPTX and other compatible formats:

Step 1: Load the Presentation

Begin by loading the presentation. Provide the file path and password if the presentation is password-protected.

// Load Presentation
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
loadOptions.setPassword("P@$$w0Rd");

Editor editor = new Editor(new FileInputStream("path/presentation.pptx"), loadOptions);

Step 2: Edit PPT/PPTX Presentation Slides with Java

After loading, modify the presentation as needed. For instance, in the following Java code, I’m replacing occurrences of the word “documents” with “presentation” in a PPTX presentation.

// Edit Presentation
Editor editor = new Editor(new FileInputStream("path/presentation.pptx"), loadOptions);
PresentationEditOptions editOptions = new PresentationEditOptions();
editOptions.setSlideNumber(0); //1st slide
editOptions.setShowHiddenSlides(true);

EditableDocument beforeEdit = editor.edit(editOptions);
String originalContent = beforeEdit.getContent();
List<IHtmlResource> allResources = beforeEdit.getAllResources();

String editedContent = originalContent.replace("document", "presentation");

Step 3: Save the Edited PowerPoint Presentation with Options

When saving the edited content, you have the flexibility to set various options. These options include setting a password and configuring output format settings. In the code snippet below, I demonstrate how to apply these options and save the edited presentation as a password-protected PPTX file.

// Save Presentation
EditableDocument afterEdit = EditableDocument.fromMarkup(editedContent, allResources);
PresentationSaveOptions saveOptions = new PresentationSaveOptions(PresentationFormats.Pptm);
saveOptions.setPassword("new_pa$$word");

editor.save(afterEdit, new ByteArrayOutputStream(), saveOptions);

try(OutputStream outputFile = new FileOutputStream("path/edited-presentation.pptx")) {
    outputStream.writeTo(outputFile);
}

Complete Java Code Example

For your convenience, here’s the complete Java code that was explained above. This code demonstrates how to edit a PowerPoint presentation and save it in PPTX format.

After running the above code, the output presentation will look like the image below. In this edited presentation, all occurrences of the word ‘documents’ have been replaced with ‘presentation’.

edited pptx presentation using editing API

Output presentation - ‘documents’ occurrences are replaced by ‘presentation’

Feel free to test out the code and see the changes for yourself! If you have any questions or need further assistance, don’t hesitate to ask.

Conclusion

In summary, we explored how to edit presentation slides in Java using a presentation editing API. This API allows you to visually edit your presentations using WYSIWYG editors. With this knowledge, you can create your own presentation editor or integrate the editing feature directly into your Java application.

For in-depth information, additional options, and examples, you can refer to the documentation and the GitHub repository. If you have any further questions, feel free to reach out to the support team on the forum.