Merge Cells

GemBox.Document supports merged cells in the same way as HTML - every table cell contains RowSpan and ColumnSpan properties.

Following sample demonstrates how to create a table with merged cells (RowSpan and / or ColumnSpan greater than 1).

Screenshot

Merge Cells 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            new TableRow(document,
18                new TableCell(document, new Paragraph(document, "Cell 1-1")),
19                new TableCell(document, new Paragraph(document, "Cell 1-2")),
20                new TableCell(document, new Paragraph(document, "Cell 1-3")),
21                new TableCell(document, new Paragraph(document, "Cell 1-4"))),
22            new TableRow(document,
23                new TableCell(document, new Paragraph(document, "Cell 2-1")),
24                new TableCell(document, new Paragraph(document, "Cell 2-2 -> 2-4"))
25                {
26                    ColumnSpan = 3
27                }),
28            new TableRow(document,
29                new TableCell(document, new Paragraph(document, "Cell 3-1")),
30                new TableCell(document, new Paragraph(document, "Cell 3-2 -> 5-3"))
31                {
32                    ColumnSpan = 2,
33                    RowSpan = 3
34                },
35                new TableCell(document, new Paragraph(document, "Cell 3-4 -> 5-4"))
36                {
37                    RowSpan = 3
38                }),
39            new TableRow(document,
40                new TableCell(document, new Paragraph(document, "Cell 4-1"))),
41            new TableRow(document,
42                new TableCell(document, new Paragraph(document, "Cell 5-1"))));
43
44        table.TableFormat.DefaultCellPadding = new Padding(10, 4);
45        table.TableFormat.PreferredWidth = new TableWidth(350, TableWidthUnit.Point);
46
47        document.Sections.Add(new Section(document, table));
48
49        document.Save("Merge Cells.docx");
50    }
51}
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                            New TableRow(document,
17                                New TableCell(document,
18                                    New Paragraph(document, "Cell 1-1")),
19                                New TableCell(document,
20                                    New Paragraph(document, "Cell 1-2")),
21                                New TableCell(document,
22                                    New Paragraph(document, "Cell 1-3")),
23                                New TableCell(document,
24                                    New Paragraph(document, "Cell 1-4"))),
25                            New TableRow(document,
26                                New TableCell(document,
27                                    New Paragraph(document, "Cell 2-1")),
28                                New TableCell(document,
29                                    New Paragraph(document, "Cell 2-2 -> 2-4")) With {
30                                     .ColumnSpan = 3
31                                    }),
32                            New TableRow(document,
33                                New TableCell(document,
34                                    New Paragraph(document, "Cell 3-1")),
35                                New TableCell(document,
36                                    New Paragraph(document, "Cell 3-2 -> 5-3")) With {
37                                     .ColumnSpan = 2,
38                                     .RowSpan = 3
39                                    },
40                                New TableCell(document,
41                                    New Paragraph(document, "Cell 3-4 -> 5-4")) With {
42                                     .RowSpan = 3
43                                    }),
44                            New TableRow(document,
45                                New TableCell(document,
46                                    New Paragraph(document, "Cell 4-1"))),
47                            New TableRow(document,
48                                New TableCell(document,
49                                    New Paragraph(document, "Cell 5-1"))))
50
51        table.TableFormat.DefaultCellPadding = New Padding(10, 4)
52        table.TableFormat.PreferredWidth = New TableWidth(350, TableWidthUnit.Point)
53
54        document.Sections.Add(New Section(document, table))
55
56        document.Save("Merge Cells.docx")
57
58    End Sub
59
60End Module

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