Free Support Forum -

How to substitute merge string with rich text in MS Word document by Aspose Server


I am Salesforce Developer. We have integrated Salesforce with Aspose to merge JSON string with Microsoft template document we send. All merging is happening correct except for “Rich Text”.

In salesforce, we have a field named “Description” of type “rich text”.We gave some value to this field and it looks like as shown in the attachment (RichTextValue.JPG).In the salesforce database, it is stored as following

  • This interaction contains all te the information about HCP, Attendees, Consultant and So on.
  • Budget can be allocated to this interaction.

Following is a sample screen shot attached with it.

User-added image

Then, While we want to merge this rich text value in a MS Word document, We send document template with merge string (Ex: {{ description }}) and the database stored rich text value as JSON String to the aspose server.After merging, if we see the document, only HTML source is shown instead of HTML content (as same as shown in the attachment).

Can you please give me ideas about how to render the rich text value correctly in merge string?.If there is any sample apex code to do this, please let me know.


Hi Vignesh,

Please see the following code that demonstrate how to mail merge HTML data into a document:

public void MailMergeInsertHtml()


Document doc = new Document(MyDir + “MailMerge.InsertHtml.doc”);<o:p></o:p>

// Add a handler for the MergeField event.<o:p></o:p>

doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();<o:p></o:p>

// Load some Html from file.<o:p></o:p>

StreamReader sr = File.OpenText(MyDir + “MailMerge.HtmlData.html”);<o:p></o:p>

string htmltext = sr.ReadToEnd();<o:p></o:p>


// Execute mail merge.<o:p></o:p>

doc.MailMerge.Execute(new string[] { “htmlField1” }, new string[] { htmltext });<o:p></o:p>

// Save resulting document with a new name.<o:p></o:p>

doc.Save(MyDir + “MailMerge.InsertHtml Out.doc”);<o:p></o:p>


private class HandleMergeFieldInsertHtml : IFieldMergingCallback<o:p></o:p>




/// This is called when merge field is actually merged with data in the document.<o:p></o:p>

/// <o:p></o:p>

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)<o:p></o:p>


// All merge fields that expect HTML data should be marked with some prefix, e.g. ‘html’.<o:p></o:p>

if (e.DocumentFieldName.StartsWith(“somefield”))<o:p></o:p>


// Insert the text for this merge field as HTML data, using DocumentBuilder.<o:p></o:p>

DocumentBuilder builder = new DocumentBuilder(e.Document);<o:p></o:p>



// The HTML text itself should not be inserted.<o:p></o:p>

// We have already inserted it as an HTML.<o:p></o:p>

e.Text = “”;<o:p></o:p>



void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)<o:p></o:p>


// Do nothing.<o:p></o:p>



Best regards,

Hi Awais,

Thanks for your reply.Whatever code you showed is in C++ or java. I am not certain about this. Can you please give me sample code in Apex language as this language is only supported in Salesforce.

Hi Vignesh,

Thanks for your inquiry. The code I shared with you is in C# language. Unfortunately, I don’t know how to convert it to Apex language. However, I have simplified above code so that you can directly place Html string in merge field without mail merge operation. I hope, this will be easier for you to translate to Apex.

Document doc = new Document(filePath);

DocumentBuilder builder = new DocumentBuilder(doc);


builder.InsertHtml("html string");


Best regards,

Hi Awais,

Thanks for your reply. As per your suggestion , we need to read the file and identify rich text block and convert into correct format. But we are not storing file locally anywhere.

We are using aspose following way:

1. Create JSON string of data which want to populate on document which rich text as well
2. Pass JSON string and file name in followingservice:
Endpoint=,EmptyParagraphs,UnusedFields,UnusedRegions,RemoveTitleRow,RemoveTitleRowInInnerTables&appSID=333f1d9e-ecdf-4a49-b1e3-ac9bf494c61e&signature=AUjUARO73jZcYPwl2dWrAEjRp%2BI, Method=POST]
3.In step 2, merge document will be created after service call and save on aspose itself
4. Access the created file using file name and get the file data in blob format and save into attachment

I wanted to know , is there any way to create merge document with correct rich text format on aspose itself?

Hi Vicky,

I have attached sample JSON file and Word document here for your reference. Please try mail merge with regions on this document. Hope, this helps.

Best regards,

2 posts were split to a new topic: HTML is not getting rendered in word document instead it is showing html tags