How to apply style to table header using C#

Hello,

If you look at the word document attached I have a table with a header containing rowspans except for the last column that doesn’t have a rowspan applied to it. I want the dark blue background to be applied to the entire header including the last column that doesn’t have a rowspan, but currently that doesn’t happen and so the second cell in the last column remains white.

Is there a way that I can get the header style to apply to the entire rowspan header? Or is it possible to get the header color from the table style and manually apply that color to any cell I want using aspose?


Thanks,

Nathan Noreiga

Hi Nathan,

Thanks for your query. In your case, please use the following code snippet for your requirement. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir +   “HasInstructionsHasTemplateTest6.docx”);
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
Row firstRow = table.FirstRow;
foreach (Cell cell in firstRow.Cells)
{
    cell.CellFormat.Shading.BackgroundPatternColor = Color.Blue;
}
Row Row = table.Rows[1];
foreach (Cell cell in Row.Cells)
{
    cell.CellFormat.Shading.BackgroundPatternColor = Color.Blue;
}
doc.Save(MyDir + @"AsposeOut.docx");

ClearShading Works Good for Watermark but,Watermark issue (Aspose).docx (15.3 KB)

We Cannot ClearShading since we want First Row/Header of Table as Background Back Color.

I used below code to Set BackgroundColor for First row alone but that fails.

Row firstRow = table.FirstRow;
foreach (Cell cell in firstRow.Cells)
{
    builder.CellFormat.Shading.BackgroundPatternColor = Color.Black;
    builder.Font.Color = Color.White;
}
// After we insert all the data from the current record we can end the table row.
builder.EndRow();

@NanthiniSenthil123 You should code like the following:

Row firstRow = table.FirstRow;
foreach (Cell cell in firstRow.Cells)
{
    cell.CellFormat.Shading.BackgroundPatternColor = Color.Black;
    foreach (Run run in cell.GetChildNodes(NodeType.Run, true))
        run.Font.Color = Color.White;
}