Read the complete article on finding and replacing text in PowerPoint presentations using C#: https://blog.aspose.com/2021/08/12/find-and-replace-text-in-powerpoint-using-csharp/
Last active
November 15, 2021 13:49
-
-
Save aspose-com-gists/d3e06dfe06c9cebddb252bc1a2f30f52 to your computer and use it in GitHub Desktop.
Find and Replace Text in PowerPoint Presentation using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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