Read the complete article on finding and replacing text in PowerPoint presentations using Java:
Created
August 13, 2021 10:56
-
-
Save aspose-com-gists/cb7908656afd853ddb7164d27bd4ce1b to your computer and use it in GitHub Desktop.
Find and Replace Text in PowerPoint Presentation using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load presentation | |
Presentation pres = new Presentation("mytextone.pptx"); | |
String strToFind = "search string"; | |
String strToReplaceWith = "replace string"; | |
// Loop through each slide | |
for (ISlide slide : pres.getSlides()) { | |
// Get all text frames in the slide | |
ITextFrame[] tf = SlideUtil.getAllTextBoxes(slide); | |
for (int i = 0; i < tf.length; i++) | |
for (IParagraph para : tf[i].getParagraphs()) | |
for (IPortion port : para.getPortions()) | |
// Find text to be replaced | |
if (port.getText().contains(strToFind)) { | |
// Replace exisitng text with the new text | |
String str = port.getText(); | |
int idx = str.indexOf(strToFind); | |
String strStartText = str.substring(0, idx); | |
String strEndText = str.substring(idx + strToFind.length(), | |
str.length() - 1 - (idx + strToFind.length() - 1)); | |
port.setText(strStartText + strToReplaceWith + strEndText); | |
} | |
} | |
// Save the presentation | |
pres.save("myTextOneAspose.pptx", SaveFormat.Pptx); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment