Merge Field Format

Hi There,

I use Nested Merge Fields to generate a word document. I would like to format one of the merge field based on the value it holds. For Example, if the value is 1, the merge field should display a text “LOW” in Green color, if 2 display text “MEDIUM” in Yellow, If 3 “HIGH” in Red.

Merge fields in Template:
TableStart:T1


TableEnd:T1

Please suggest how it could be done.

Thanks
arnisen

Hi Ken,

Thanks for your inquiry. In your case, I suggest you please implement IFieldMergingCallback interface as shown in following code snippet. Please read following documentation links for your kind reference. Hope this helps you. Please let us know if you have any more queries.
https://reference.aspose.com/words/net/aspose.words.mailmerging/ifieldmergingcallback/
https://docs.aspose.com/words/net/types-of-mail-merge-operations/

private class HandleMergeFields : IFieldMergingCallback
{
    /// 
    /// This is called when merge field is actually merged with data in the document.
    /// 
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        if (e.FieldName == "FieldName")
        {
            DocumentBuilder builder = new DocumentBuilder(e.Document);
            builder.MoveToMergeField(e.DocumentFieldName);
            if (e.FieldValue == "1")
            {
                builder.Font.Color = Color.Green;
                builder.Writeln("LOW");
            }
            else if (e.FieldValue == "2")
            {
                builder.Font.Color = Color.Yellow;
                builder.Writeln("MEDIUM");
            }
            e.Text = "";
        }
    }
    void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
    {
        // Do nothing.
    }
}