Aspose.Pdf allows developers to create tables in PDF documents. Moreover, other effects like setting its border style, margins and Cell paddings can also be applied on the tables. Before going into more technical details, it's important to understand the concepts of border, margins and padding which are presented below in a diagram:
|
In the above figure, you can see that the borders of Table , Row and Cell are overlapped. Using Aspose.Pdf , Table can have margins and Cells can have paddings. To set Cell margins, we have to set Cell paddings.
Borders
To set the borders of Table , Row and Cell you can use Table.Border , Row.Border and Cell.Border properties of Table , Row and Cell classes respectively. But borders of cells can also be set using DefaultCellBorder property of Table class. All border related properties discussed above are assigned an instance of BorderInfo class, which is created by calling its constructor. BorderInfo Constructor has many overloads that take almost all parameters required to customize the border.
Margins Or Padding
To set the margins or padding of a cell we can use Padding property of Cell class. But padding of cells can also be managed using DefaultCellPadding property of Table class. All padding related properties are assigned an instance of MarginInfo class that takes information about Left , Right , Top and Bottom parameters for creating customized margins.
In the following example, width of Cell borders is set to 0.1 point, width of Table border is set to 1 point and Cell paddings are set to 5 points.
Code Snippet
[C#]
//Instantiate a table object
Table tab1 = new Table();
//Add the table in paragraphs collection of the desired section
sec1.Paragraphs.Add(tab1);
//Set with column widths of the table
tab1.ColumnWidths = "50 50 50";
//Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new BorderInfo((int)BorderSide.All,0.1F);
//Set table border using another customized BorderInfo object
tab1.Border = new BorderInfo((int)BorderSide.All,1F);
//Create MarginInfo object and set its left, bottom, right and top margins
MarginInfo margin=new MarginInfo();
margin.Top=5f;
margin.Left=5f;
margin.Right=5f;
margin.Bottom=5f;
//Set the default cell padding to the MarginInfo object
tab1.DefaultCellPadding=margin;
//Create rows in the table and then cells in the rows
Row row1 = tab1.Rows.Add();
row1.Cells.Add("col1");
row1.Cells.Add("col2");
row1.Cells.Add("col3");
Row row2 = tab1.Rows.Add();
row2.Cells.Add("item1");
row2.Cells.Add("item2");
row2.Cells.Add("item3");
[VB.NET]
'Instantiate a table object
Dim tab1 As Table = New Table()
'Add the table in paragraphs collection of the desired section
sec1.Paragraphs.Add(tab1)
'Set with column widths of the table
tab1.ColumnWidths = "50 50 50"
'Set default cell border using BorderInfo object
tab1.DefaultCellBorder = New BorderInfo(BorderSide.All, 0.1F)
'Set table border using another customized BorderInfo object
tab1.Border = New BorderInfo(BorderSide.All, 1.0F)
'Create MarginInfo object and set its left, bottom, right and top margins
Dim margin As MarginInfo = New MarginInfo()
margin.Top=5f
margin.Left=5f
margin.Right=5f
margin.Bottom=5f
'Set the default cell padding to the MarginInfo object
tab1.DefaultCellPadding=margin
'Create rows in the table and then cells in the rows
Dim row1 As Row = tab1.Rows.Add()
row1.Cells.Add("col1")
row1.Cells.Add("col2")
row1.Cells.Add("col3")
Dim row2 As Row = tab1.Rows.Add()
row2.Cells.Add("item1")
row2.Cells.Add("item2")
row2.Cells.Add("item3")
[JAVA]
//Instantiate a table object
Table table = new Table(sec1);
//Add the table in paragraphs collection of the desired section
sec1.getParagraphs().add(table);
//Set with column widths of the table
table.setColumnWidths("50 50 50");
//Set default cell border using BorderInfo object
table.setDefaultCellBorder(new BorderInfo(BorderSide.All, 0.1F));
//Set table border using another customized BorderInfo object
table.setBorder(new BorderInfo(BorderSide.All, 1F));
//Create MarginInfo object and set its left, bottom, right and top margins
MarginInfo margin = new MarginInfo(5f, 5f, 5f, 5f);
//Set the default cell padding to the MarginInfo object
table.setDefaultCellPadding(margin);
//Create rows in the table and then cells in the rows
Row row1 = table.getRows().add();
row1.getCells().add("col1");
row1.getCells().add("col2");
row1.getCells().add("col3");
Row row2 = table.getRows().add();
row2.getCells().add("item1");
row2.getCells().add("item2");
row2.getCells().add("item3");
[XML]
<Table ColumnWidths="50 50 50" DefaultCellPaddingTop="5"
DefaultCellPaddingLeft="5" DefaultCellPaddingRight="5"
DefaultCellPaddingBottom="5">
<DefaultCellBorder>
<All LineWidth="0.1"></All>
</DefaultCellBorder>
<Border>
<All LineWidth="1"></All>
</Border>
<Row>
<Cell>
<Text>
<Segment>col1</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>col2</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>col3</Segment>
</Text>
</Cell>
</Row>
<Row>
<Cell>
<Text>
<Segment>item1</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>item2</Segment>
</Text>
</Cell>
<Cell>
<Text>
<Segment>item3</Segment>
</Text>
</Cell>
</Row>
</Table>
The generated table is shown in the figure below:
|
Figure: Demonstration of Table |