Simple Table

A Table is a document element used to arrange content in rows and columns. With a table it's easy to organize and display large amounts of data in a grid like structure.

GemBox.Document's table concept is same as in most tables, like the ones you can find in Word or HTML documents. It's comprised of cells, rows and columns.

The following example shows how you can create and populate a simple table.

Word document with Table, Rows and Cells
Screenshot of Word file with simple table
using GemBox.Document;
using GemBox.Document.Tables;

class Program
    static void Main()
        // If using Professional version, put your serial key below.
        int rowCount = %RowCount%;
        int columnCount = %ColumnCount%;

        var document = new DocumentModel();

        var section = new Section(document);

        // Create a table with 100% width.
        var table = new Table(document);
        table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);

        for (int r = 0; r < rowCount; r++)
            // Create a row and add it to table.
            var row = new TableRow(document);

            for (int c = 0; c < columnCount; c++)
                // Create a cell and add it to row.
                var cell = new TableCell(document);

                // Create a paragraph and add it to cell.
                var paragraph = new Paragraph(document, $"Cell ({r + 1},{c + 1})");

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

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.

        Dim rowCount As Integer = %RowCount%
        Dim columnCount As Integer = %ColumnCount%

        Dim document As New DocumentModel()

        Dim section As New Section(document)

        ' Create a table with 100% width.
        Dim table As New Table(document)
        table.TableFormat.PreferredWidth = New TableWidth(100, TableWidthUnit.Percentage)

        For r As Integer = 0 To rowCount - 1

            ' Create a row and add it to table.
            Dim row As New TableRow(document)

            For c As Integer = 0 To columnCount - 1

                ' Create a cell and add it to row.
                Dim cell As New TableCell(document)

                ' Create a paragraph and add it to cell.
                Dim paragraph As New Paragraph(document, $"Cell ({r + 1},{c + 1})")



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

    End Sub
End Module

You can create a nested table by adding a new table into a cell's TableCell.Blocks collection.

You can also create a table from an HTML source (e.g. <table>, <tr>, <td> tags) by using the ContentRange.LoadText or ContentPosition.LoadText methods, like shown in the Insert HTML and RTF content to Word file example.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.