Hi,
This is my first time working on table borders.
I saw that there are borders divided into multiple levels:
I also saw that the BorderCollection object has direct-accessible properties and a property that can be accessed via loop. What the differences?
I would access to this border section on word:
image.png (2.8 KB)
Can someone give me support?
Thanks in advice,
F.
@federico.altamura You can set table borders in different ways.
Apply specific border line:
Table table = doc.FirstSection.Body.Tables[0];
table.ClearBorders();
table.SetBorder(BorderType.Left, LineStyle.Single, 1.5, Color.Black, true);
Apply all borders:
Table table = doc.FirstSection.Body.Tables[0];
table.ClearBorders();
table.SetBorders(LineStyle.Single, 1.5, Color.Black);
Thus, the borders are applied to the row level that is above the cell level. It’s the same as:
foreach (Row row in table.Rows)
{
BorderCollection borders = row.RowFormat.Borders;
borders.LineStyle = LineStyle.Single;
borders.LineWidth = 1.5;
borders.Color = Color.Black;
}
If you need to set different borders for each cell, first you need to clear the borders at the row level, if any, and then apply borders to the cell.
Clear borders at the row level:
table.ClearBorders();
or
foreach (Row row in table.Rows)
row.RowFormat.Borders.ClearFormatting();
Apply borders at the cell level:
foreach (Row row in table.Rows)
{
foreach (Cell cell in row.Cells)
{
BorderCollection borders = cell.CellFormat.Borders;
borders.LineStyle = LineStyle.Single;
borders.LineWidth = 1.5;
borders.Color = Color.Black;
}
}
You can find more in our documentation.