Word template using mustache syntax add html

Hi Team

I have word template in which I am using mustache syntax
If I want to add HTML into that word dox how I can do that
Please help me on it

Sample code I am using

DataSet? myDataSet = new DataSet();
using (StreamReader streamReader = new StreamReader(report.JsonDataFile))
{
    string json = streamReader.ReadToEnd();
    myDataSet = JsonConvert.DeserializeObject<DataSet>(json);
}
doc.MailMerge.ExecuteWithRegions(myDataSet);
doc.Save(memoryStreamPdfOutput, SaveFormat.docx);

Other then data set how to pass HTML to show into Word file?

@gauravgarg04 You can use IFieldMergingCallback to insert HMTL data upon executing mail merge. Please see the second code example in the above link.

Hi @alexey.noskov

The example you given is using fields
In my case I am using mustache

In your example below is code (here we only passing fields)

doc.MailMerge.Execute(new[] { "html_Title", "html_Body" }, mergeData);

In my case it’s (Here we passing data set)

 doc.MailMerge.ExecuteWithRegions(myDataSet);

I need to execute my with “ExecuteWithRegions” function

@gauravgarg04 Yes, the example is for simple mail merge with regular merge fields in the template. But the approach will work with mustache syntax and mail merge with regions too.

@alexey.noskov
Can you give me a sample as I am not able to find relevant example and not able to implement the same

@alexey.noskov also the example you shared saying “Called when a mail merge merges data into a MERGEFIELD”

But in case of mustache synatax we are not using MERGEFIELD

Please help me on that on urgent bases
we need to finalize this reporting tool ASAP

@gauravgarg04 Here is a simple code example that uses mustache syntax:

DataTable data = new DataTable("MyTable");
data.Columns.Add("field1");
data.Columns.Add("field2");
data.Rows.Add("<b>bold text</b>", "some text");
data.Rows.Add("<i>italic text</i>", "some text");

// this is a sample temlate. For demonstration purposes I generate template on the fly.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("{{#foreach MyTable}}{{field1}}\t\t{{field2}}{{/foreach MyTable}}");

// Set field merging callback.
doc.MailMerge.FieldMergingCallback = new HandleMergeFieldInsertHtml();
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.ExecuteWithRegions(data);

// Save the output
doc.Save(@"C:\Temp\out.docx");
private class HandleMergeFieldInsertHtml : IFieldMergingCallback
{
    /// <summary>
    /// Called when a mail merge merges data into a MERGEFIELD.
    /// </summary>
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs args)
    {
        // Suppose field1 contains HTML content
        if (args.DocumentFieldName == "field1")
        {
            // Add parsed HTML data to the document's body.
            DocumentBuilder builder = new DocumentBuilder(args.Document);
            builder.MoveToMergeField(args.DocumentFieldName);
            builder.InsertHtml((string)args.FieldValue);

            // Since we have already inserted the merged content manually,
            // we will not need to respond to this event by returning content via the "Text" property. 
            args.Text = string.Empty;
        }
    }

    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}

Here is the output document produced: out.docx (7.1 KB)

@alexey.noskov
I tried but css is not working,

@gauravgarg04 Could you please provide the problematic HTML string here for testing.
Also, please note that HTML and MS Word documents are different in model and it is difficult and sometimes impossible to provide 100% fidelity upon conversion from one format to another.