MS PowerPoint provides a useful feature of finding and replacing text in the presentations. However, you might need automation to perform this operation for a batch of files. Accordingly, in this article, you will learn how to find and replace text in PowerPoint PPTX/PPT programmatically using Java.
Java API to Find and Replace Text in PowerPoint
In order to find and replace text in PPTX/PPT presentations, we’ll use Aspose.Slides for Java. It is a powerful API to create, manipulate, and convert PowerPoint presentations from within your Java applications. You can either download the API or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>21.7</version>
<classifier>jdk16</classifier>
</dependency>
Find and Replace Text in PowerPoint PPTX using Java
The following are the steps to find and replace text in a PPTX presentation using Java.
- First, load the PowerPoint presentation using Presentation class.
- Then, loop through each ISlide in the presentation.
- In each iteration, get text frames in an ITextFrame array.
- For each ITextFrame, perform the following operations:
- Loop through the IParagraphCollection in each text frame.
- Access the IPortionCollection in each IParagraph.
- Access the text of each IPortion using IPortion.getText() method and check if it contains the search string.
- If yes, find the position of the search string and replace it using IPortion.setText() method.
- Finally, save the updated presentation using Presentation.save(string, SaveFormat) method.
The following code sample shows how to find and replace text in a PowerPoint presentation.
Get a Free API License
You can use Aspose.Slides for Java without evaluation limitations by requesting a temporary license.
Conclusion
In this article, you have learned how to find and replace text in PowerPoint presentations programmatically using Java. You can simply integrate the API and the provided code sample in your Java applications. In addition, you can visit the documentation to explore other features of Aspose.Slides for Java. Furthermore, you can let us know about your queries via our forum.