Upload
Maximum file size: 1 MB. Files accepted: ppt, pptx.
Click here to browse files.
fileerrors
Convert to
Source file:
filename
Search Text:
Replace Text:
downloads
- Demo
- Java
- C# source
This demo shows you how to search specific text in a PowerPoint document and replace the matched text.
package ppt; import com.spire.presentation.*; import java.awt.*; import java.util.HashMap; import java.util.Map; public class FindAndReplaceDemo { public void findOrReplace(String filePath,String changeType, String text, String newText, String resultFilePath) throws Exception { Presentation presentation = new Presentation(); presentation.loadFromFile(filePath); switch (changeType){ case "FIND": findText(presentation,text); break; case "REPLACE": replaceText(presentation,text,newText); break; } presentation.saveToFile(resultFilePath,FileFormat.PPTX_2013); } private void findText(Presentation presentation, String text){ for(int i = 0; i < presentation.getSlides().getCount();i++){ ISlide slide = presentation.getSlides().get(i); for (int j = 0; j < slide.getShapes().getCount();j++){ IAutoShape shape = (IAutoShape)slide.getShapes().get(j); TextHighLightingOptions options = new TextHighLightingOptions(); options.setWholeWordsOnly(true); options.setCaseSensitive(true); shape.getTextFrame().highLightText(text, Color.yellow, options); } } } private void replaceText(Presentation presentation, String oldText, String newText){ for(int i = 0; i < presentation.getSlides().getCount();i++){ ISlide slide = presentation.getSlides().get(i); Map map = new HashMap<>(); map.put(oldText,newText); replaceTags(slide,map); } } private static void replaceTags(ISlide pSlide, Map tagValues) { for (int i = 0; i < pSlide.getShapes().getCount(); i++) { IShape curShape = pSlide.getShapes().get(i); if (curShape instanceof IAutoShape) { for (int j = 0; j < ((IAutoShape) curShape).getTextFrame().getParagraphs().getCount(); j++) { ParagraphEx tp = ((IAutoShape) curShape).getTextFrame().getParagraphs().get(j); for (Map.Entry entry : tagValues.entrySet()) { String mapKey = entry.getKey(); String mapValue = entry.getValue(); if (tp.getText().contains(mapKey)) { tp.setText(tp.getText().replace(mapKey, mapValue)); } } } } } } }
using Spire.Presentation; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DemoOnlineCode { class HighlightOrReplace { public void HighlightOrReplaceDemo(string filePath, string format, string text, string newText, string resultFileName) { Presentation presentation = new Presentation(); presentation.LoadFromFile(filePath); switch (format) { case "HIGHLIGHT": HighlightText(presentation,text,resultFileName); break; case "REPLACE": ReplaceText(presentation, text, newText, resultFileName); break; } } private static void HighlightText(Presentation presentation, string text, string resultFileName) { foreach (ISlide slide in presentation.Slides) { foreach (IShape shape in slide.Shapes) { if (shape is IAutoShape) { IAutoShape autoShape = shape as IAutoShape; TextHighLightingOptions options = new TextHighLightingOptions(); options.WholeWordsOnly = true; options.CaseSensitive = true; autoShape.TextFrame.HighLightText(text, Color.Yellow, options); } } } presentation.SaveToFile(resultFileName+".pptx", FileFormat.Pptx2013); } private static void ReplaceText(Presentation presentation, string oldText, string newText, string resultFileName) { Dictionary tagValues = new Dictionary(); tagValues.Add(oldText,newText); foreach (ISlide slide in presentation.Slides) { ReplaceTags(slide, tagValues); } presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013); } private static void ReplaceTags(ISlide pSlide, Dictionary TagValues) { foreach (IShape curShape in pSlide.Shapes) { if (curShape is IAutoShape) { foreach (TextParagraph tp in (curShape as IAutoShape).TextFrame.Paragraphs) { foreach (var curKey in TagValues.Keys) { if (tp.Text.Contains(curKey)) { tp.Text = tp.Text.Replace(curKey, TagValues[curKey]); } } } } } } } }
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the function you want, please request a free demo from us.