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
using Aspose.Slides;
using Aspose.Slides.Export;
using Aspose.Slides.Util;
static void findReplaceText(string strToFind, string strToReplaceWith)
{
//Open the presentation
using Presentation pres = new Presentation("mytextone.ppt");
//Get all text frames in the presentation
ITextFrame[] textFrames = SlideUtil.GetAllTextFrames(pres, false);
for (int i = 0; i < textFrames.Length; i++)
foreach (IParagraph para in textFrames[i].Paragraphs)
foreach (IPortion port in para.Portions)
{
string str = port.Text;
int idx = str.IndexOf(strToFind);
//Find text to be replaced
if (idx >= 0)
//Replace exisitng text with the new text, the portion formatting is kept
{
string strStartText = str.Substring(0, idx);
string strEndText = str.Substring(idx + strToFind.Length);
port.Text = strStartText + strToReplaceWith + strEndText;
}
}
pres.Save("myTextOneAspose.ppt", SaveFormat.Ppt);
}