I'm currently trying to replace some text in a .ppt using Spire.Presentation.
It works well, but it seems to set the color of the text in the resulting .ppt to black, no matter what the original color was.
What am I doing wrong?
I have this code at the moment:
- Code: Select all
private static void ReplaceTags(ISlide pSlide, Dictionary<string, string> replacements)
{
foreach (IShape curShape in pSlide.Shapes)
{
if (curShape is IAutoShape)
{
foreach (TextParagraph tp in (curShape as IAutoShape).TextFrame.Paragraphs)
{
// For a "weird" reason, some properties are not kept when replacing text, so we must backup them
TextFont font = tp.FirstTextRange.LatinFont;
float fontHeight = tp.FirstTextRange.FontHeight;
TriState isBold = tp.FirstTextRange.IsBold;
TriState isItalic = tp.FirstTextRange.IsItalic;
System.Drawing.Color color = tp.FirstTextRange.TextLineFormat.FillFormat.SolidFillColor.Color;
foreach (KeyValuePair<string, string> kvp in replacements)
{
if (tp.Text.Contains(kvp.Key))
{
tp.Text = tp.Text.Replace(kvp.Key, kvp.Value);
}
}
tp.FirstTextRange.LatinFont = font;
tp.FirstTextRange.FontHeight = fontHeight;
tp.FirstTextRange.IsBold = isBold;
tp.FirstTextRange.IsItalic = isItalic;
tp.FirstTextRange.TextLineFormat.FillFormat.SolidFillColor.Color = color;
}
}
}
}
In this code, I'm trying to replace specific texts in the given slide.
For example, if the slide contains "I enjoy walking in the rain", and my dictionary is { { "enjoy", "hate" }, { "rain", "mud" } }, the resulting text should be "I hate walking in the mud".
You can see that I'm actually dealing with another problem.
I noticed that when changing text, these properties were not kept: font, font height, and bold and italic states.
And, of course, the color of the text, which is my main problem since my code solved the other problems.
The line "tp.FirstTextRange.TextLineFormat.FillFormat.SolidFillColor.Color = color;" seems to have no effect.
I really feel like I'm not using the methods properly.
Any help would be greatly appreciated!
Thanks,
Arthur