We have learnt so far that how to create tables and how to control their behaviors. But we have not tried one thing yet. Yes, how about adding a Table into another Table ? Such Table is called Nested Table. Well, it sounds good that Aspose.Pdf also provides this feature to developers to meet their different kinds of needs.
To add a nested Table , all you need is to add a new Table into the Paragraphs collection of any desired Cell .
Code Snippet
[C#]
//Create a table
Table tab1 = new Table();
//Add the table into the paragraphs collection of section
sec1.Paragraphs.Add(tab1);
//Set the column widths of the table
tab1.ColumnWidths = "100 200";
//Set the default cell border using BorderInfo instance
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All);
//Add a row into the table
Row row1 = tab1.Rows.Add();
//Add 1st cell in the row
row1.Cells.Add("left cell");
//Add 2nd cell in the row
Cell cell2 = row1.Cells.Add();
//Create a table to be nested with the reference of 2nd cell in the row
Table tab2 = new Table(cell2);
//Add the nested table into the paragraphs collection of the 2nd cell
cell2.Paragraphs.Add(tab2);
//Set the column widths of the nested table
tab2.ColumnWidths = "100 100";
//Create 1st row in the nested table
Row row21 = tab2.Rows.Add();
//Create 1st cell in the 1st row of the nested table
Cell cell21 = row21.Cells.Add("top cell");
//Set the column span of the 1st cell (in the 1st row of the nested table) to 2
cell21.ColumnsSpan = 2;
//Create 2nd row in the nested table
Row row22 = tab2.Rows.Add();
//Create 1st cell in the 2nd row of the nested table
row22.Cells.Add("left bottom cell");
//Create 2nd cell in the 2nd row of the nested table
row22.Cells.Add("right bottom cell");
[VB.NET]
'Create a table
Dim tab1 As Table = New Table()
'Add the table into the paragraphs collection of section
sec1.Paragraphs.Add(tab1)
'Set the column widths of the table
tab1.ColumnWidths = "100 200"
'Set the default cell border using BorderInfo instance
tab1.DefaultCellBorder = New BorderInfo(BorderSide.All)
'Add a row into the table
Dim row1 As Row = tab1.Rows.Add()
'Add 1st cell in the row
row1.Cells.Add("left cell")
'Add 2nd cell in the row
Dim cell2 As Cell = row1.Cells.Add()
'Create a table to be nested with the reference of 2nd cell in the row
Dim tab2 As Table = New Table(cell2)
'Add the nested table into the paragraphs collection of the 2nd cell
cell2.Paragraphs.Add(tab2)
'Set the column widths of the nested table
tab2.ColumnWidths = "100 100"
'Create 1st row in the nested table
Dim row21 As Row = tab2.Rows.Add()
'Create 1st cell in the 1st row of the nested table
Dim cell21 As Cell = row21.Cells.Add("top cell")
'Set the column span of the 1st cell (in the 1st row of the nested table) to 2
cell21.ColumnsSpan = 2
'Create 2nd row in the nested table
Dim row22 As Row = tab2.Rows.Add()
'Create 1st cell in the 2nd row of the nested table
row22.Cells.Add("left bottom cell")
'Create 2nd cell in the 2nd row of the nested table
row22.Cells.Add("right bottom cell")
[JAVA]
//Create a table
Table tab1 = new Table(sec1);
//Add the table into the paragraphs collection of section
sec1.getParagraphs().add(tab1);
//Set the column widths of the table
tab1.setColumnWidths("100 200");
//Set the default cell border using BorderInfo instance
tab1.setDefaultCellBorder( new BorderInfo(BorderSide.All) );
//Add a row into the table
Row row1 = tab1.getRows().add();
//Add 1st cell in the row
row1.getCells().add("left cell");
//Add 2nd cell in the row
Cell cell2 = row1.getCells().add();
//Create a table to be nested with the reference of 2nd cell in the row
Table tab2 = new Table(sec1);
//Add the nested table into the paragraphs collection of the 2nd cell
cell2.addNestedTable(tab2);
//Set the column widths of the nested table
tab2.setColumnWidths("100 100");
//Create 1st row in the nested table
Row row21 = tab2.getRows().add();
//Create 1st cell in the 1st row of the nested table
Cell cell21 = row21.getCells().add("top cell");
//Set the column span of the 1st cell (in the 1st row of the nested table) to 2
cell21.setColumnSpan(2);
//Create 2nd row in the nested table
Row row22 = tab2.getRows().add();
//Create 1st cell in the 2nd row of the nested table
row22.getCells().add("left bottom cell");
//Create 2nd cell in the 2nd row of the nested table
row22.getCells().add("right bottom cell");
[XML]
<Table ColumnWidths="100 200">
<DefaultCellBorder>
<All LineWidth="1"/>
</DefaultCellBorder>
<Row>
<Cell>
<Text>
<Segment>left cell</Segment>
</Text>
</Cell>
<Cell>
<Table ColumnWidths="100 100">
<Row>
<Cell ColumnsSpan="2">
<Text>
<Segment>top cell</Segment>
</Text>
</Cell>
</Row>
<Row>
<Cell>
<Text>
<Segment>left bottom cell</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>right bottom cell</Segment>
</Text>
</Cell>
</Row>
</Table>
</Cell>
</Row>
</Table>
The above code will produce the output table as shown in the fegure below:
|
Figure: Demonstration of Nested Table |