Replace placeholder with merge field

1 Answer 518 Views
WordsProcessing
Missing User
Missing User asked on 13 Dec 2021, 02:03 PM

Hi,

I'm developing a template creation application so users can create their own templates and such for mail merge purposes and I am wondering if there is any way to replace placeholders (for example "[order:id]") with merge fields using RadFlowDocumentEditor (replace text feature to be exact).

I already realized ReplaceText does not support FieldInfo and I know I can replace the placeholder with values from the database as well but I think there is probably a way to get actual templates from that.

Thanks in advance.

1 Answer, 1 is accepted

Sort by
1
Accepted
Dimitar
Telerik team
answered on 14 Dec 2021, 08:28 AM

Hello David,

This is possible. You will need to manually add the field start/end. Here is an example: 

var provider = new DocxFormatProvider();
var doc = provider.Import(File.ReadAllBytes(@"..\..\sample.docx"));

var editor = new RadFlowDocumentEditor(doc);

FieldInfo field = new FieldInfo(doc);

List<InlineBase> fieldInlines = new List<InlineBase>();
fieldInlines.Add(field.Start);
fieldInlines.Add(new Run(doc) { Text = "MERGEFIELD LastName" });
fieldInlines.Add(field.Separator);
fieldInlines.Add(new Run(doc) { Text = "" });
fieldInlines.Add(field.End);

editor.ReplaceText("Field", fieldInlines);

File.WriteAllBytes(@"..\..\result.docx", provider.Export(doc));

More information is available in the following articles: 

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/.

Tags
WordsProcessing
Asked by
Missing User
Answers by
Dimitar
Telerik team
Share this question
or