WordProcessing - ReplaceText doesn't work properly

2 Answers 192 Views
WordsProcessing
Luigi
Top achievements
Rank 1
Iron
Luigi asked on 01 Apr 2022, 10:29 AM

Hi everyone,

in my company we are using Telerik WordProcessing to handle some docx templates by replacing placeholder in there with values we took from DB. Everything works well when we use ReplaceText of RadFlowDocumentEditor except when we have a string that contains '\n' character.

Basically, for a string like "Ipsum\nDolor\nSit\nAmet" when we call ReplaceText on a particular placeholder, we got a situation like this

Ipsum
Dolor
Sit
Amet

which seems look ok, but unfortunately just the first row keeps the font style that placeholder has, the others instead doesn't and fallback to default style.

Any idea how to solve this?

Thank you very much for your attention

2 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 04 Apr 2022, 11:04 AM

Hi Luigi,

You can create a paragraph that contains the desired content by cloning the original run and that replace the placeholder. Here is an example: 

var result = editor.FindAll("#Placeholder").First();

var paragraph = new Paragraph(document);
var run1 = result.Runs.First().Clone(); 
paragraph.Inlines.Add(run1);
run1.Text = "Ipsum";
paragraph.Inlines.Add(new Break(document) {  BreakType = BreakType.LineBreak });

var run2 = result.Runs.First().Clone();
paragraph.Inlines.Add(run2);
run2.Text = "Dolor";
paragraph.Inlines.Add(new Break(document) { BreakType = BreakType.LineBreak });

var run3 = result.Runs.First().Clone();
paragraph.Inlines.Add(run3);
run3.Text = "Sit";
paragraph.Inlines.Add(new Break(document) { BreakType = BreakType.LineBreak });

var run4 = result.Runs.First().Clone();
paragraph.Inlines.Add(run4);
run4.Text = "Amet";

editor.ReplaceText("#Placeholder", paragraph);

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Luigi
Top achievements
Rank 1
Iron
answered on 05 Apr 2022, 07:45 AM

Thank you so much Dimitar for your answer. I tried and I can say you It works.

Thanks again

Tags
WordsProcessing
Asked by
Luigi
Top achievements
Rank 1
Iron
Answers by
Dimitar
Telerik team
Luigi
Top achievements
Rank 1
Iron
Share this question
or