Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Thu Mar 22, 2018 10:01 am

Hello,
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

eldoir
 
Posts: 3
Joined: Thu Nov 30, 2017 1:57 pm

Fri Mar 23, 2018 9:32 am

Hello Arthur,

Thanks for your post. After an initial test with the latest version( Spire.Presentation Pack Hotfix Version:3.3.0), I was unable to reproduce the issue. The style of the font could remain after replacement and you don't need to set the style manually again. Please use the version and refer to the below code to give it a try.
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)
                    {             
                        foreach (KeyValuePair<string, string> kvp in replacements)
                        {
                            if (tp.Text.Contains(kvp.Key))
                            {
                                tp.Text = tp.Text.Replace(kvp.Key, kvp.Value);
                            }
                        }
                    }
                }
            }
        }


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Fri Mar 23, 2018 10:24 am

Hello Simon,

This code is part of a (way) bigger project and I'm not allowed to update any of the references in the project.
We are currently using FreeSpire.Office v2.15.0.
With the code you posted (it was actually my original code), I'm indeed losing all the properties I previously mentioned.
Is it a bug in this specific version?

Thanks for you help!

eldoir
 
Posts: 3
Joined: Thu Nov 30, 2017 1:57 pm

Fri Mar 23, 2018 10:40 am

Hello,

Thanks for your reply. Yes, the free version does have the issue. As we don't have plan to update the free version, you need to turn to the full version(Spire.Office Platinum (DLL Only) Version:2.16.27) which doesn't have the issue and you don't need to set the style again.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Fri Mar 23, 2018 10:49 am

Ok then, I will evaluate my possibilities.
Thanks again for your quick replies!

eldoir
 
Posts: 3
Joined: Thu Nov 30, 2017 1:57 pm

Mon Mar 26, 2018 1:31 am

Hello,

Thanks for your reply. If there is any queries, just feel free to contact us.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.Presentation