Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/cb7908656afd853ddb7164d27bd4ce1b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/cb7908656afd853ddb7164d27bd4ce1b to your computer and use it in GitHub Desktop.
Find and Replace Text in PowerPoint Presentation using Java

Read the complete article on finding and replacing text in PowerPoint presentations using Java:

// 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