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

Hi,

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

Hi,

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

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.

Thanks.

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");
    // Add a handler for the MergeField event.
    doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
    // Load some Html from file.
    StreamReader sr = File.OpenText(MyDir + "MailMerge.HtmlData.html");
    string htmltext = sr.ReadToEnd();
    sr.Close();
    // Execute mail merge.
    doc.MailMerge.Execute(new string[] { "htmlField1" }, new string[] { htmltext });
    // Save resulting document with a new name.
    doc.Save(MyDir + "MailMerge.InsertHtml Out.doc");
}
private class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
    /// 

    /// This is called when merge field is actually merged with data in the document.
    /// 
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        // All merge fields that expect HTML data should be marked with some prefix, e.g. ‘html’.
        if (e.DocumentFieldName.StartsWith("somefield"))
        {
            // Insert the text for this merge field as HTML data, using DocumentBuilder.
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.DocumentFieldName);
            builder.InsertHtml((string)e.FieldValue);
            // The HTML text itself should not be inserted.
            // We have already inserted it as an HTML.
            e.Text = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}

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.MoveToMergeField("mf");
builder.InsertHtml("html string");
doc.Save("out.docx");

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=https://api.aspose.com/v1.1/words/FileName/executeTemplate?cleanup=ContainingFields,EmptyParagraphs,UnusedFields,UnusedRegions,RemoveTitleRow,RemoveTitleRowInInnerTables&appSID=333f1d9e-ecdf-4a49-b1e3-ac9bf494c61e&signature=AUjUARO73jZcYPwl2dWrAEjRp%2BI, Method=POST]
  1. In step 2, merge document will be created after service call and save on aspose itself
  2. 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?

Regards,
Vicky

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