Support Center

How to remove Table borders

There are two ways you can define Table borders in a Word document. One way is to define the borders in a Table element, and another way is to define the borders in TableCell elements.
The GemBox.Document supports both of these options and offers the following properties for their representation:

For demonstration purposes, we will create a new Table element and set its TableFormat borders and TableCellFormat borders.

C# code

var document = new DocumentModel();
var table = new Table(document, 3, 3,
                (int r, int c) => new TableCell(document,
                    new Paragraph(document, string.Format("Cell[{0},{1}]", r, c))));
table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);

var tableBorders = table.TableFormat.Borders;
tableBorders.SetBorders(MultipleBorderTypes.Inside, BorderStyle.None, Color.Empty, 0);
tableBorders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Triple, Color.Green, 1.8);

var tableCellBorders = table.Rows[1].Cells[1].CellFormat.Borders;
tableCellBorders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Double, Color.Red, 1);

document.Sections.Add(new Section(document, table));
document.Save("Table Sample.docx");

VB.NET code

Dim document = New DocumentModel()
Dim table = New Table(document, 3, 3, _
                Function(r As Integer, c As Integer) New TableCell(document, _
                    New Paragraph(document, String.Format("Cell[{0},{1}]", r, c))))
table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)

Dim tableBorders = table.TableFormat.Borders
tableBorders.SetBorders(MultipleBorderTypes.Inside, BorderStyle.None, Color.Empty, 0)
tableBorders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Triple, Color.Green, 1.8)

Dim tableCellBorders = table.Rows(1).Cells(1).CellFormat.Borders
tableCellBorders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Double, Color.Red, 1)

document.Sections.Add(New Section(document, table))
document.Save("Table Sample.docx")

The following is the resulting "Table Sample.docx" file:

Table Sample.docx

Now to remove all the Table’s borders, we are required to set every defined border in the targeted Table element to BorderStyle none. This includes both TableFormat borders and TableCellFormat borders.

C# code

var targetedTable = document.GetChildElements(true, ElementType.Table).Cast<Table>().First();
targetedTable.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.None, Color.Empty, 0);
foreach (var row in targetedTable.Rows)
    foreach (var cell in row.Cells)
        cell.CellFormat.Borders.ClearBorders();

document.Save("Borderless Table Sample.docx");

VB.NET code

Dim targetedTable = document.GetChildElements(True, ElementType.Table).Cast(Of Table)().First()
targetedTable.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.None, Color.Empty, 0)
For Each row As TableRow In targetedTable.Rows
    For Each cell As TableCell In row.Cells
        cell.CellFormat.Borders.ClearBorders()
    Next
Next

document.Save("Borderless Table Sample.docx")

The following is the resulting "Borderless Table Sample.docx" file:

Borderless Table Sample.docx

3 of 6 people found this page helpful
Subscribe to this article to get an email notification when it is updated.

0 Comments

  • There are no comments.