We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

ExecuteWithRegions - Change font color

Hi Support,

I’m using ExecuteWithRegions (ASPOSE.Word v17x), how can I change the font color to red for the negative values after the mail merge.
https://www.screencast.com/t/XtSFp4TlxT

Sample Code: https://drive.google.com/file/d/1oRBrf8hXIHtsY5wuPK9akWSnPL78WHNR/view?usp=sharing

Any help is greatly appreciated!!

Vijay

@VijaySripada,

Please use the following simple code to meet this requirement:

public class HandleMergeField : IFieldMergingCallback
{
    void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    {
        double val = Convert.ToDouble(e.FieldValue);
                
        if (val < 0)
        {
            DocumentBuilder builder = new DocumentBuilder(e.Document);                    
            builder.MoveToMergeField(e.FieldName);
            builder.Font.Color = Color.Red;
            builder.Write(val.ToString());
        }
    }

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

DataSet ds = new DataSet();
ds.ReadXml("D:\\Temp\\Alternating_colors\\rawdata.xml");

Document doc = new Document("D:\\Temp\\Input-new.docx");

doc.MailMerge.FieldMergingCallback = new HandleMergeField();
doc.MailMerge.ExecuteWithRegions(ds);

doc.Save("D:\\temp\\18.8.docx");

Thanks @awais.hafeez