Problem with background color in cells

Hi there,

I am trying to create a simple table with multiple cells and use yellow as the background color. below is my code snippet, when I pass in 2 strings, this piece of code should create a table with 2 cells in the same row, and set the background color of both cells to yellow, but when I output to a file, only first cell is in yellow, the second cell is still white, any clue why this is happening? I’ve attached my screenshot.

builder.StartTable();
builder.RowFormat.Height = 15;
builder.RowFormat.HeightRule = HeightRule.Exactly;
builder.RowFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;

builder.CellFormat.Width = pageWidth / columns.Length;
builder.CellFormat.WrapText = true;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

foreach(string s in columns)
{
    builder.InsertCell();
    builder.Font.Bold = false;
    builder.Font.Size = 13;
    builder.Write(s);
}
builder.Font.ClearFormatting();
builder.CellFormat.ClearFormatting();

builder.EndRow();
builder.RowFormat.ClearFormatting();
builder.EndTable();

Hi
Thanks for your request. Please modify your code as shown below:

builder.StartTable();
builder.RowFormat.Height = 15;
builder.RowFormat.HeightRule = HeightRule.Exactly;
builder.RowFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;
builder.CellFormat.Width = pageWidth / columns.Length;
builder.CellFormat.WrapText = true;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;
foreach(string s in columns)
{
    builder.InsertCell();
    builder.Font.Bold = false;
    builder.Font.Size = 13;
    builder.Write(s);
}
builder.EndRow();
builder.EndTable();
// Clear all formats after we closed the table.
builder.Font.ClearFormatting();
builder.CellFormat.ClearFormatting();
builder.RowFormat.ClearFormatting();

Best regards,

that worked, thank you, could you briefly explain the reason?

Hi
Thanks for your request. When you clear formatting before closing the table, DocumentBuilder clears the formatting of the current cell. This causes the problem. So you have to clear the formatting when you moved outside the cell, i.e. after closing the table.
Best regards,