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 Sep 08, 2022 10:55 am

**Problem:**
I have a single textbox with 4 tags which I want to replace with something else but I want the new text to retain the original formatting. Each tag is formatted a bit differently (i.e. font type, size, bold etc).

I want to replace these tags with these values:
{{test}}: 43.15
{{see}}: 1.34
{{format}}: 16.74
{{retained}}: 41.02

This is what my slide template looks like
Image

This is what I am expecting:
Image

Here's my pptx
3477940d-b710-4f1d-ab3b-af0350ccf3a9 (2).zip


How do I do this?

majeed_s
 
Posts: 69
Joined: Thu Mar 25, 2021 4:13 pm

Fri Sep 09, 2022 6:06 am

Hello majeed_s,

Thanks for your inquiry.
Please refer to the below tutorial sample code to achieve your demand. If you have any other questions, please feel free to contact us.

https://www.e-iceblue.com/Tutorials/Spi ... -in-C.html

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Fri Sep 09, 2022 11:09 am

Thank you for replying. This is what we are doing but the problem is it does not retain the font formatting.

Code: Select all
(item as IAutoShape).TextFrame.Paragraphs[0].Text = (item as IAutoShape).TextFrame.Paragraphs[0].Text.Replace(key, value)

majeed_s
 
Posts: 69
Joined: Thu Mar 25, 2021 4:13 pm

Tue Sep 13, 2022 8:23 am

Hello majeed_s,

Thanks for your feedback.
Yes, your code will not retain the font formatting, please use the code below to do the replacements, and I have attached my result document for your reference. If there is any other issues, just feel free to write back.

Code: Select all
 
            //Create PPT document and load file
            Presentation presentation = new Presentation();
            presentation.LoadFromFile(@"Test1.pptx");

            presentation.Slides[0].ReplaceFirstText("{{test}}", "43.15", true);

            presentation.Slides[0].ReplaceAllText("{{see}} ", "1.34", true);

            presentation.Slides[0].ReplaceAllText("{{formatting}} ", "16.74", true);

            presentation.Slides[0].ReplaceAllText("{{retained}}", "41.02", true);

            String result = "ReplaceTextRetentionStyle_result.pptx";

            //Save to file.
            presentation.SaveToFile(result, FileFormat.Pptx2013);


Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Thu Sep 15, 2022 8:28 am

Thanks for the code snippet.

majeed_s
 
Posts: 69
Joined: Thu Mar 25, 2021 4:13 pm

Fri Sep 16, 2022 9:35 am

Hello majeed_s,

Thanks for your feedback.
You are welcome. If you have any further questions, please feel free to contact us.

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Return to Spire.Presentation