Developers can gain more control over table cells by setting their widths and cells span. Aspose.Pdf provides simplest API to do these tasks. Let's understand these concepts step by step with examples:
Cell width
Cell width is the width between the axes of the cell borders which is shown in the following figure:
|
It is important to understand the concepts of cell width especially when it is needed to add an Image into a Cell . In that case, the width of image should be fixed according to the width of the cell to make it look good. The width of the Image can be fixed by setting the ImageInfo.FixWidth property of Image class like this:
image.ImageInfo.FixWidth = cellWidth - cellBorderWidth -
cell.Padding.Left - cell.Padding.Right;
If ImageInfo.FixWidth property is set, the Image is scaled to the fixed width.
Column Widths
There are two approaches provided by Aspose.Pdf that allow developers to set column widths which are described below with examples.
First approach is to set ColumnWidths property of Table class to a string containing the widths of all columns in the Table . The width value of each column should be separated by a blank space. The default unit is point but centimeter (cm) and inches (inch) are also supported.
Code Snippet
[C#]
table1.ColumnWidths = "70 2cm";
[VB.NET]
table1.ColumnWidths = "70 2cm"
[JAVA]
table.setColumnWidths("70 2cm");
[XML]
<Table ColumnWidths="70 2cm">
...
</Table>
Second approach is to set FitWidth property of the Cell class. The default unit is also point but centimeter (cm) and inches (inch) can be used too.
Note: If ColumnWidths property of Table class is set then FitWidth property of the Cell object is not needed to set.
Code Snippet
[C#]
cell1.FitWidth = 2;
[VB.NET]
cell1.FitWidth = 2
[XML]
<Cell FitWidth="2">
...
</Cell>
Using the flexible API of Aspose.Pdf , you can set the width of each cell. The maximum width, a Cell can have in a column, is equal to the column width.
Columns Span
When working with tables, it's very common for us to make a column span more than 1 columns. Mostly, we create such columns to be used as headings for covering more child columns under them.
The Cell.ColumnsSpan is used to extends cells on a horizontal row (left and right). The following figure shows a Cell that spans 2 columns.
|
Code Snippet
[C#]
cell1.ColumnsSpan = 2;
[VB.NET]
cell1.ColumnsSpan = 2
[JAVA]
cell1.setColumnSpan(2);
[XML]
<Cell ColumnsSpan = "2">
...
</Cell>