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

Free Support Forum - aspose.com

Alternating row colors for lots of different tables

I have a document with a title page and then additional photo pages. The title page has a table, but I don’t want to color it’s rows. After the title page are more pages, each with an attributes table. I’d like to apply alternate row coloring to each of these attributes tables.

«TableStart:PhotoAttributes»«AttributeName» «AttributeValue»«TableEnd:PhotoAttributes»

The example code I tried assumes you only have one table in the document. Seems like I need to be able to locate a particular table in the document. Or, skip over the first table on the title page. It would be nice if the row color restarted for each table.

How can I accomplish this?

Attached is my sample project with the FieldMergingCallback set up.


Hi Brian,
Thanks for your inquiry.
You’re right, that example code it a bit limiting if you have more than one table. I have written a quick implementation which works with the rows of the table directly. It alternates the colors of the rows in the Attributes tables during merging. Please see the code below:

void IFieldMergingCallback.FieldMerging(FieldMergingArgs e)
    if (e.FieldName.Equals("AttributeName"))
        // The row which contains this mergefield.
        Row row = (Row) e.Field.Start.GetAncestor(NodeType.Row);
        // The parent table of this row
        Table table = row.ParentTable;
        // Find the index of the row in the table.
        // Subtract 1 off the index of the row in order to take into account the header row.
        int remainder = (table.IndexOf(row) - 1) % 2;
        foreach(Cell cell in row.Cells)
            if (remainder == 0)
                // Even row
                cell.CellFormat.Shading.BackgroundPatternColor = Color.Blue;
                // Odd row
                cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue;

If you have any other inqueries, please feel free to ask.

That worked great! Thanks!