Find and Replace in Presentation
Contents
[
Hide
]
Following are the steps to follow:
- Open a presentation.
- Search the text.
- Replace the text.
- Write the presentation.
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.Util;
string filePath = @"..\..\..\Sample Files\";
string strToFind = "old text";
string strToReplaceWith = "new text";
//Open the presentation
using (Presentation pres = new Presentation(filePath + "Find and Replace.pptx"))
{
//Get all text boxes in the first slide
ITextFrame[] textFrames = SlideUtil.GetAllTextBoxes(pres.Slides[0]);
foreach (ITextFrame textFrame in textFrames)
foreach (IParagraph para in textFrame.Paragraphs)
foreach (IPortion port in para.Portions)
{
//Find the text to be replaced
int idx = port.Text.IndexOf(strToFind);
if (idx < 0)
continue;
//Replace the existing text with the new text
string str = port.Text;
string strStartText = str.Substring(0, idx);
string strEndText = str.Substring(idx + strToFind.Length);
port.Text = strStartText + strToReplaceWith + strEndText;
}
pres.Save(filePath + "Find and Replace.pptx", SaveFormat.Pptx);
}