Find and Replace Text without Losing Format in Presentation
Contents
[
Hide
]
Both methods follow these steps:
- Open a presentation.
- Search the text.
- Replace the text.
- Write the presentation.
VSTO
private void findReplaceText(string strToFind, string strToReplaceWith)
{
//Open the presentation
PowerPoint.Presentation pres = null;
pres = Globals.ThisAddIn.Application.Presentations.Open("mytextone.ppt",
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse);
//Loop through slides
foreach (PowerPoint.Slide sld in pres.Slides)
//Loop through all shapes in slide
foreach (PowerPoint.Shape shp in sld.Shapes)
{
//Access text in the shape
string str = shp.TextFrame.TextRange.Text;
//Find text to replace
if (str.Contains(strToFind))
//Replace exisitng text with the new 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));
shp.TextFrame.TextRange.Text = strStartText + strToReplaceWith + strEndText;
}
pres.SaveAs("MyTextOne___.ppt",
PowerPoint.PpSaveAsFileType.ppSaveAsPresentation,
Microsoft.Office.Core.MsoTriState.msoFalse);
}
Aspose.Slides
private static void findReplaceText(string strToFind, string strToReplaceWith)
{
//Open the presentation
Presentation pres = new Presentation("mytextone.ppt");
//Get all text boxes in the presentation
ITextBox[] tb = PresentationScanner.GetAllTextBoxes(pres, false);
for (int i = 0; i < tb.Length; i++)
foreach (Paragraph para in tb[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;
}
pres.Write("myTextOneAspose.ppt");
}