This article demonstrates how to replace text in an exising PowerPoint document with new text using Spire.Presentation for Java.
import com.spire.presentation.*; import java.util.HashMap; import java.util.Map; public class ReplaceText { public static void main(String[] args) throws Exception { //create a Presentation object Presentation presentation = new Presentation(); //load the template file presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx"); //get the first slide ISlide slide= presentation.getSlides().get(0); //create a Map object Map map = new HashMap(); //add several pairs of keys and values to the map map.put("#name#","John Smith"); map.put("#age#","28"); map.put("#address#","Oklahoma City, United States"); map.put("#tel#","333 123456"); map.put("#email#","[email protected]"); //replace text in the slide replaceText(slide,map); //save to another file presentation.saveToFile("output/ReplaceText.pptx", FileFormat.PPTX_2013); } /** * Replace text within a slide * @param slide Specifies the slide where the replacement happens * @param map Where keys are existing strings in the document and values are the new strings to replace the old ones */ public static void replaceText(ISlide slide, Map map) { for (Object shape : slide.getShapes() ) { if (shape instanceof IAutoShape) { for (Object paragraph : ((IAutoShape) shape).getTextFrame().getParagraphs() ) { ParagraphEx paragraphEx = (ParagraphEx)paragraph; for (String key : map.keySet() ) { if (paragraphEx.getText().contains(key)) { paragraphEx.setText(paragraphEx.getText().replace(key, map.get(key))); } } } } } } }