Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active November 15, 2021 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/d3e06dfe06c9cebddb252bc1a2f30f52 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/d3e06dfe06c9cebddb252bc1a2f30f52 to your computer and use it in GitHub Desktop.
Find and Replace Text in PowerPoint Presentation using C#
// Load presentation
Presentation pres = new Presentation("mytextone.pptx");
string strToFind = "search string";
string strToReplaceWith = "replace string";
// Loop through each slide
foreach (Slide slide in pres.Slides)
{
// Get all text frames in the slide
ITextFrame[] tf = SlideUtil.GetAllTextBoxes(slide);
for (int i = 0; i < tf.Length; i++)
foreach (Paragraph para in tf[i].Paragraphs)
foreach (Portion port in para.Portions)
// Find text to be replaced
if (port.Text.Contains(strToFind))
{
// Replace exisitng text with the new text
string str = port.Text;
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.Text = 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