Replace text

I am replacing text in the textframe of a shape.


The text frame has a heading that is in bold. It is also in a different colour to the rest of the text.

The text changes fine but the font size, colour and other characteristics all get changed when the slide gets saved. All of the text becoems bold etc…taking on the characteristics of the heading.

Please help.

Here is some sample code I am using…


public static void ReplaceTextInShape(string documentNameAndPath, int slideNumber, string shapeName, string oldText, string newText)
{
Aspose.Slides.Presentation presentation = new Presentation(documentNameAndPath);

Aspose.Slides.ISlide slide = presentation.Slides[slideNumber];

Aspose.Slides.IShape shape = slide.Shapes.FirstOrDefault(t => t.Name == shapeName);

if (shape != null)
{
((IAutoShape)shape).TextFrame.Text = ((IAutoShape)shape).TextFrame.Text.Replace(oldText, newText);
}
presentation.Save(documentNameAndPath, Aspose.Slides.Export.SaveFormat.Pptm);
presentation.Dispose();
}

Hi Jason,

Thanks for inquiring Aspose.Slides.

I have observed your requirements of replacing text. Actually, you are replacing text on TextFrame level and when you do so the text formatting properties are lost. In order to retain the text properties, you need to change the text on portion level which will ensure the text formatting properties as per requirement. Please try using following alternate on your end.

public static void ReplaceTextInShape(string documentNameAndPath, int slideNumber, string shapeName, string oldText, string newText)
{
Aspose.Slides.Presentation presentation = new Presentation(documentNameAndPath);

Aspose.Slides.ISlide slide = presentation.Slides[slideNumber];

Aspose.Slides.IShape shape = slide.Shapes.FirstOrDefault(t => t.Name == shapeName);

if (shape != null)
{
IAutoShape ashp=(IAutoShape)shape;
if(ashp.TextFrame!=null)
{
ITextFrame text = ashp.TextFrame;
foreach (IParagraph para in text.Paragraphs)
{
foreach (IPortion port in para.Portions)
{
port.Text.Replace(oldText, newText);
}
}
}

}

presentation.Save(documentNameAndPath, Aspose.Slides.Export.SaveFormat.Pptm);
presentation.Dispose();
}

Many Thanks,