Table Formatting

In GemBox.Document, table related formatting options can be specified on a table, table rows and table cells.

Following sample shows how to create a formatted table.

Screenshot

Table Formatting Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using System;
2using System.Data;
3using GemBox.Document;
4using GemBox.Document.Tables;
5
6class Sample
7{
8    [STAThread]
9    static void Main(string[] args)
10    {
11        // If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
13
14        DocumentModel document = new DocumentModel();
15
16        Table table = new Table(document);
17        table.TableFormat.AutomaticallyResizeToFitContents = false;
18        table.TableFormat.Alignment = HorizontalAlignment.Center;
19
20        table.Columns.Add(new TableColumn() { PreferredWidth = 50 });
21        table.Columns.Add(new TableColumn() { PreferredWidth = 80 });
22        table.Columns.Add(new TableColumn() { PreferredWidth = 110 });
23        table.Columns.Add(new TableColumn() { PreferredWidth = 140 });
24
25        TableRow row = new TableRow(document);
26        row.RowFormat.Height = new TableRowHeight(100, TableRowHeightRule.AtLeast);
27        table.Rows.Add(row);
28
29        TableCell cell1 = new TableCell(document, new Paragraph(document, "Cell 1-1"));
30        cell1.CellFormat.TextDirection = TableCellTextDirection.TopToBottom;
31        cell1.CellFormat.Padding = new Padding(5, 10);
32        row.Cells.Add(cell1);
33
34        TableCell cell2 = new TableCell(document, new Paragraph(document, "Cell 1-2"));
35        cell2.CellFormat.VerticalAlignment = VerticalAlignment.Center;
36        row.Cells.Add(cell2);
37
38        row.Cells.Add(new TableCell(document, new Paragraph(document, "Cell 1-3"))
39            {
40                CellFormat = new TableCellFormat()
41                {
42                    BackgroundColor = Color.Red
43                }
44            });
45
46        row.Cells.Add(new TableCell(document, new Paragraph(document, "Cell 1-4")
47            {
48                ParagraphFormat = new ParagraphFormat()
49                {
50                    Alignment = HorizontalAlignment.Center
51                }
52            })
53            {
54                CellFormat = new TableCellFormat()
55                {
56                    VerticalAlignment = VerticalAlignment.Center,
57                    BackgroundColor =  Color.Yellow
58                }
59            });
60
61        document.Sections.Add(new Section(document, table));
62
63        document.Save("Table Formatting.docx");
64    }
65}
1Imports System
2Imports System.Data
3Imports GemBox.Document
4Imports GemBox.Document.Tables
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Dim document As DocumentModel = New DocumentModel
14
15        Dim table As New Table(document)
16        table.TableFormat.AutomaticallyResizeToFitContents = False
17        table.TableFormat.Alignment = HorizontalAlignment.Center
18
19        table.Columns.Add(New TableColumn() With {.PreferredWidth = 50})
20        table.Columns.Add(New TableColumn() With {.PreferredWidth = 80})
21        table.Columns.Add(New TableColumn() With {.PreferredWidth = 110})
22        table.Columns.Add(New TableColumn() With {.PreferredWidth = 140})
23
24        Dim row As New TableRow(document)
25        row.RowFormat.Height = New TableRowHeight(100, TableRowHeightRule.AtLeast)
26        table.Rows.Add(row)
27
28        Dim cell1 As New TableCell(document, New Paragraph(document, "Cell 1-1"))
29        cell1.CellFormat.TextDirection = TableCellTextDirection.TopToBottom
30        cell1.CellFormat.Padding = New Padding(5, 10)
31        row.Cells.Add(cell1)
32
33        Dim cell2 As New TableCell(document, New Paragraph(document, "Cell 1-2"))
34        cell2.CellFormat.VerticalAlignment = VerticalAlignment.Center
35        row.Cells.Add(cell2)
36
37        row.Cells.Add(
38            New TableCell(document,
39                New Paragraph(document, "Cell 1-3")) With {
40                    .CellFormat = New TableCellFormat() With {
41                        .BackgroundColor = Color.Red
42         }})
43
44        row.Cells.Add(
45            New TableCell(document,
46                New Paragraph(document, "Cell 1-4") With {
47                    .ParagraphFormat = New ParagraphFormat() With {
48                        .Alignment = HorizontalAlignment.Center
49                 }
50        }) With {
51            .CellFormat = New TableCellFormat() With {
52                .VerticalAlignment = VerticalAlignment.Center,
53                .BackgroundColor = Color.Yellow
54         }})
55
56        document.Sections.Add(New Section(document, table))
57
58        document.Save("Table Formatting.docx")
59
60    End Sub
61
62End Module

Check next sample or find out more about GemBox.Document and GemBox Software.