Table Formatting

With GemBox.Document you can specify the formatting options on a table, row and cell using the TableFormat, TableRowFormat and TableCellFormat classes.

You have various formatting options at your disposal, like table width, row height, cell alignment, cell spacing and many more.

The following example demonstrates some available table, row and cell formatting options.

Screenshot of Word file with table formatting
Word document with table, row and cell formatting set with GemBox.Document
using GemBox.Document;
using GemBox.Document.Tables;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        var table = new Table(document);
        table.TableFormat.AutomaticallyResizeToFitContents = false;

        // By default Table has assigned "Table Grid" style, the same as when creating it in Microsoft Word.
        // This base style defines borders which can be removed with the following.
        table.TableFormat.Style.TableFormat.Borders.ClearBorders();

        // Add columns with specified width.
        table.Columns.Add(new TableColumn(60));
        table.Columns.Add(new TableColumn(120));
        table.Columns.Add(new TableColumn(180));

        // Add rows with specified height.
        table.Rows.Add(new TableRow(document) { RowFormat = { Height = new TableRowHeight(30, TableRowHeightRule.AtLeast) } });
        table.Rows.Add(new TableRow(document) { RowFormat = { Height = new TableRowHeight(60, TableRowHeightRule.AtLeast) } });
        table.Rows.Add(new TableRow(document) { RowFormat = { Height = new TableRowHeight(90, TableRowHeightRule.AtLeast) } });

        for (int r = 0; r < 3; r++)
            for (int c = 0; c < 3; c++)
            {
                // Add cell.
                var cell = new TableCell(document);
                table.Rows[r].Cells.Add(cell);

                // Set cell's vertical alignment.
                cell.CellFormat.VerticalAlignment = (VerticalAlignment)r;

                // Add cell content.
                var paragraph = new Paragraph(document, $"Cell ({r + 1},{c + 1})");
                cell.Blocks.Add(paragraph);

                // Set cell content's horizontal alignment.
                paragraph.ParagraphFormat.Alignment = (HorizontalAlignment)c;

                if ((r + c) % 2 == 0)
                {
                    // Set cell's background and borders.
                    cell.CellFormat.BackgroundColor = new Color(255, 242, 204);
                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Double, Color.Red, 1);
                }
            }

        document.Sections.Add(new Section(document, table));

        document.Save("Table Formatting.%OutputFileType%");
    }
}
Imports GemBox.Document
Imports GemBox.Document.Tables

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As New DocumentModel()

        Dim table As New Table(document)
        table.TableFormat.AutomaticallyResizeToFitContents = False

        ' By default Table has assigned "Table Grid" style, the same as when creating it in Microsoft Word.
        ' This base style defines borders which can be removed with the following.
        table.TableFormat.Style.TableFormat.Borders.ClearBorders()

        ' Add columns with specified width.
        table.Columns.Add(New TableColumn(60))
        table.Columns.Add(New TableColumn(120))
        table.Columns.Add(New TableColumn(180))

        ' Add rows with specified height.
        table.Rows.Add(New TableRow(document) With {.RowFormat = New TableRowFormat() With {.Height = New TableRowHeight(30, TableRowHeightRule.AtLeast)}})
        table.Rows.Add(New TableRow(document) With {.RowFormat = New TableRowFormat() With {.Height = New TableRowHeight(60, TableRowHeightRule.AtLeast)}})
        table.Rows.Add(New TableRow(document) With {.RowFormat = New TableRowFormat() With {.Height = New TableRowHeight(90, TableRowHeightRule.AtLeast)}})

        For r = 0 To 2
            For c = 0 To 2

                ' Add cell.
                Dim cell As New TableCell(document)
                table.Rows(r).Cells.Add(cell)

                ' Set cell's vertical alignment.
                cell.CellFormat.VerticalAlignment = CType(r, VerticalAlignment)

                ' Add cell content.
                Dim paragraph As New Paragraph(document, $"Cell ({r + 1},{c + 1})")
                cell.Blocks.Add(paragraph)

                ' Set cell content's horizontal alignment.
                paragraph.ParagraphFormat.Alignment = CType(c, HorizontalAlignment)

                If (r + c) Mod 2 = 0 Then

                    ' Set cell's background and borders.
                    cell.CellFormat.BackgroundColor = New Color(255, 242, 204)
                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Double, Color.Red, 1)

                End If
            Next
        Next

        document.Sections.Add(New Section(document, table))

        document.Save("Table Formatting.%OutputFileType%")

    End Sub
End Module

You can prevent a row from being split across multiple pages when its content doesn't fit in a previous page by using the TableRowFormat.AllowBreakAcrossPages property.

However, if you want to prevent a table from splitting across multiple pages, then you'll need to specify that the content contained within the table should stay together. For this you can check the Keep table on same page article.

Check next example or download examples from GitHub.