Hi,
I’m looking for a way to apply custom styles to odd and even rows in a table. I’ve tried using ConditionalStyles.EvenRowBanding and ConditionalStyles.OddRowBanding in the attached example, but it seems that the resulting table doesn’t use the styles specified - could you please advise me on how I can get these styles to apply?
Thanks,
Tomek
RowBanding.zip (3.4 KB)
Hi @acturisaspose
I appreciate your interest in Aspose products.
I prepared some simple code that will help you achieve what you need.
private void Logic(Document doc)
{
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
builder.InsertCell();
builder.Writeln("Item");
builder.InsertCell();
builder.Writeln("Quantity (kg)");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Apples");
builder.InsertCell();
builder.Writeln("20");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Bananas");
builder.InsertCell();
builder.Writeln("40");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Carrots");
builder.InsertCell();
builder.Writeln("50");
builder.EndRow();
table.AutoFit(AutoFitBehavior.AutoFitToContents);
var rowList = table.Rows.Cast<Row>().ToList();
bool odd = true;
foreach (var row in rowList)
{
odd = !odd;
foreach (Cell cell in row.ChildNodes)
{
if (odd)
{
cell.CellFormat.Shading.BackgroundPatternColor = Color.Aqua;
cell.CellFormat.Shading.Texture = TextureIndex.TextureSolid;
cell.CellFormat.Borders.LineStyle = LineStyle.DotDash;
}
else
{
cell.CellFormat.Shading.BackgroundPatternColor = Color.Magenta;
cell.CellFormat.Borders.LineStyle = LineStyle.DoubleWave;
}
foreach (Run run in cell.GetChildNodes(NodeType.Run, true))
{
Aspose.Words.Font font = run.Font;
if (odd)
{
// Set some font formatting properties
font.Bold = true;
font.Color = System.Drawing.Color.Red;
font.Name = "Verdana";
}
else
{
font.Bold = false;
font.Color = System.Drawing.Color.Blue;
font.Name = "Calibri";
}
}
}
}
}
I exaggerated the styles just for the sake of viewing them. I attached the result of running the code sample.
TableStylesPerRow_output.docx (7.3 KB)
Please let me know if you would like to know something else.
Best regards.
@acturisaspose Your code is correct, but you should also specify TableStyle.RowStripe value. For example see the following simple code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Name");
builder.InsertCell();
builder.Write("Value");
builder.EndRow();
for (int i = 0; i < 10; i++)
{
builder.InsertCell();
builder.InsertCell();
builder.EndRow();
}
builder.EndTable();
TableStyle tableStyle = (TableStyle)doc.Styles.Add(StyleType.Table, "MyTableStyle1");
// Define background color to the first row of table.
tableStyle.ConditionalStyles.FirstRow.Shading.BackgroundPatternColor = Color.GreenYellow;
tableStyle.ConditionalStyles.FirstRow.Shading.Texture = TextureIndex.TextureNone;
tableStyle.ConditionalStyles.OddRowBanding.Shading.BackgroundPatternColor = Color.Gray;
tableStyle.ConditionalStyles.OddRowBanding.Shading.Texture = TextureIndex.TextureNone;
tableStyle.ConditionalStyles.EvenRowBanding.Shading.BackgroundPatternColor = Color.White;
tableStyle.ConditionalStyles.EvenRowBanding.Shading.Texture = TextureIndex.TextureNone;
tableStyle.RowStripe = 1;
table.Style = tableStyle;
doc.Save(@"C:\Temp\out.docx");
out.docx (7.2 KB)
FYI: @carlosmc
hi,
@alexey.noskov
Thanks! this works exactly as expected!
1 Like