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

Free Support Forum - aspose.com

Changing table cell background color base on merge field value

Hello, We are using word document templates to produce our reports.

We change the font color of some merge fields based on if a result is at target or not.

We do this using DocumentBuilder as follows:

If e.FieldName.Equals("cholLdlAch") Then
    'Here you can modify code according to your requirements.
    mBuilder.MoveToMergeField(e.FieldName)
    If IsDBNull(e.FieldValue) = False Then
        run.Text = e.FieldValue
        If bolCholLdlAtTarget = True Then
            font.Color = System.Drawing.Color.Green
        End If
        If bolCholLdlNotAtTarget = True Then
            font.Color = System.Drawing.Color.Orange
        End If
        If bolCholLdlNotAssessed = True Then
            font.Color = System.Drawing.Color.Orange
        End If
        font.Size = 14
        font.Name = "Calibri"
        font.Bold = True
        mBuilder.InsertNode(run)
    End If
End If

The customer now wants to change the color of the whole table cell (whole row actually) based on if a result is at target or not.

Is this possible in Aspose? If so, do you have a code sample I could view?

@joedigiulio,

Would be kind enough to send the template you are working with?

@joedigiulio,

I think CellFormat.Shading is what you need to use to change the background.

Here is a very basic simple code that uses the shading property:

private void Logic(Document doc)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    Table table = builder.StartTable();

    var cell1 = builder.InsertCell();
    builder.Writeln("Item");
    builder.InsertCell();
    builder.Writeln("Quantity (kg)");
    builder.EndRow();
    cell1.CellFormat.Shading.BackgroundPatternColor = Color.Aqua;

    var cell2 = builder.InsertCell();
    builder.Writeln("Apples");
    builder.InsertCell();
    builder.Writeln("20");
    builder.EndRow();
    cell2.CellFormat.Shading.BackgroundPatternColor = Color.Magenta;

    var cell3 = builder.InsertCell();
    builder.Writeln("Bananas");
    builder.InsertCell();
    builder.Writeln("40");
    builder.EndRow();
    cell3.CellFormat.Shading.BackgroundPatternColor = Color.Yellow;

    builder.InsertCell();
    builder.Writeln("Carrots");
    builder.InsertCell();
    builder.Writeln("50");
    builder.EndRow();

    table.AutoFit(AutoFitBehavior.AutoFitToContents);           
}

Hopefully, this will help you out.