Possible to replace text by mergefield including initial text formatting?

I have been trying to replace custom text on word document and replacing with mergefield but i can't seem to figure out how copy across the initial text formatting to the mergefield.



Any help would be appreciated.
Thanks


2 Replies

OS Ootam Seewoogoolam February 15, 2018 04:48 AM UTC

I found a solution. but if anyone has a better way to do this, please post in this thread. Thanks



MJ Mohanaselvam Jothi Syncfusion Team February 15, 2018 01:31 PM UTC

Hi Ootam,

Thank you for contacting Syncfusion support.

To achieve your requirement, we suggest you to use Replace API of DocIO. It replaces the given text with TextBodyPart and preserves the formatting from custom text.

Please refer the below code example to replace custom text with mail merge field with its formatting using DocIO:
 
//Loads a template document 
WordDocument document = new WordDocument("Template.docx", FormatType.Docx);           
//Creates new paragraph. 
WParagraph paragraph = new WParagraph(document); 
//Appends mail merge field to the paragraph. 
paragraph.AppendField("SimpleField", FieldType.FieldMergeField); 
//Initializes text body part 
TextBodyPart bodyPart = new TextBodyPart(document); 
bodyPart.BodyItems.Add(paragraph); 
 
//Replaces a particular text with the text body part, 
//taking into consideration case sensitive, whole word, and formatting options. 
document.Replace("text", bodyPart, true, true, true); 
 
//Saves and closes the document
document.Save("Sample.docx"); 
document.Close(); 


Please let us know if you have any other questions.

Regards,
Mohanaselvam J


Loader.
Up arrow icon