Tables

A table consists of one or more columns and one or more rows. Each row contains one or more cells. A cell contains plain text or any page element (see Adding Page Elements to Cells for more information on adding page elements to a cell). A table consists of a Table2 class, one or more rows (Row2 class), one or more columns (Column2 class), and one or more cells (Cell2 class).

Table2

Use the Table2 class to create a table. This class uses the Row2, Cell2, and Column2 classes to construct a table. The Table2 class has numerous properties for table-wide layout and adding rows and cells. Refer to the Table2 API documentation for a complete example.

Row2

A Table2 has a Rows property that contains the table's row. The Rows property is a Row2List that contains the actual Row2 instances.

Cell2

The Row2 class contains a Cells property that contains a row's cells. The Cells property is a Cell2List that contains the actual Cell2 instances.

Column2

A table also has a Columns property that contains the table's columns. Columns are for specifying each column's width and do not specify content. The Columns property is a Column2List that contains the actual Column2 instances.

Simple Example

A row contains one or more cells. The number of cells added to a row matches the columns added to the table. The number of columns corresponds with the number of cells. The following example illustrates creating a 2x2 table.

// Create a table 
Table2 table = new Table2(0, 0, 600, 600);

//Add two columns to the table
table.Columns.Add(150);
table.Columns.Add(90);

// Add two rows to the table
Row2 row1 = table.Rows.Add(40);
Row2 row2 = table.Rows.Add(30);
row1.Cells.Add("A1");
row1.Cells.Add("A2");
row2.Cells.Add("B1");
row2.Cells.Add("B2");

Spanning Columns And Rows

A table can span rows and columns using a Cell2 instance's RowSpan and ColumnSpan properties. For example, the following code creates a 2x4 table where the cell in the first row and first column spans two rows while the table's first row and third column span two columns.

// Create a table 
Table2 table = new Table2(0, 0, 600, 600);

//Add columns to the table
table.Columns.Add(150);
table.Columns.Add(90);
table.Columns.Add(90);
table.Columns.Add(90);

// Add rows to the table and add cells to the rows
Row2 row1 = table.Rows.Add(40);
row1.Cells.Add("A1");
row1.Cells[0].RowSpan = 2;
row1.Cells.Add("A2");
row1.Cells.Add("A3");
row1.Cells[2].ColumnSpan = 2;

Row2 row2 = table.Rows.Add(30);
row2.Cells.Add("B2");
row2.Cells.Add("B3");
row2.Cells.Add("B4");

Table Text Formatting

A table formats content within a PDF document and controls the data flow between sections or pages within that PDF (for more information, refer to Table Continuation). Formatting text (i.e., background color, font, font size, text color, and alignment) is hierarchical, where the precedence is the cell, row, and table. Each cell is handled hierarchically.

Formatting at the table level applies the formatting to every cell. However, formatting a row takes precedence over the formatting specified for the table and uses the particular row's formatting rather than the table's. Formatting a cell takes precedence over the formatting specified within a row. For example, the following example illustrates a table where the background color is red, and one of the cells overrides the table's color with the cell's color.

Table2 table = new Table2(0, 0, 600, 600);
table.BackgroundColor = RgbColor.Red;

table.Columns.Add(150);
table.Columns.Add(90);

Row2 row1 = table.Rows.Add(40);
row1.Cells.Add("A1");
row1.Cells.Add("A2");

Row2 row3 = table.Rows.Add(30);
row2.Cells.Add("B1");
row2.Cells.Add("B2");
row2.Cells[0].BackgroundColor = RgbColor.Blue;

Table Borders

By default, both Cell2 and Table2 have a border width of one. Unless the widths are changed, this will result in a combined width of two. Therefore, to achieve a border around all cells with a value of one, the Table2 and the Cell2 instances should have their width set to .5. Note that when customizing a table's appearance, you will often have to modify the widths of the different cells, as the example below, when discussing CellBorderStyle, illustrates.

Border

A Cell2 has a Border property consisting of a Border instance.

Properties
Property Description
Bottom Gets the CellBorderStyle object for the bottom border.
Color Gets or sets the Color of the border.
Left Gets the CellBorderStyle object for the left border.
LineStyle Gets or sets the LineStyle of the border.
Right Gets the CellBorderStyle object for the right border.
Top Gets the CellBorderStyle object for the top border.
Width Gets or sets the width of the border.
CellBorderStyle

The Right, Left, Top, and Bottom properties can be set to allow custom styling of a cell's right, left, top, and bottom lines by using a CellBorderStyle instance. The CellBorderStyle has a Color, LineStyle, and Width property. Setting these properties for the right, left, top, or bottom of a cell overrides the property settings for the cell.

Formatted Tablel

Figure 1. Formatted table example.

The following example illustrates customizing a table's cells. It creates a 2x3 table, where the first cell spans two rows, the second column in the first row has orange lines, the second column in the second row has dashed lines, and the third column in the second row only has a bottom and left line.

Table2 table = new Table2(0, 0, 600, 600);
table.BackgroundColor = RgbColor.Red;

//do not display lines for the table
table.Border.Width = 0;

table.Columns.Add(90);
table.Columns.Add(90);
table.Columns.Add(90);

Row2 row1 = table.Rows.Add(40);
row1.Cells.Add("A1");

//do not display right line
row1.Cells[0].Border.Right.Width = 0;
//display bottom line as is bottom of table but do not display right line
row1.Cells[0].Border.Bottom.Width = 1;
row1.Cells[0].Border.Right.Width = 0;
row1.Cells[0].RowSpan = 2;
row1.Cells.Add("A2");

//set border color to orange
row1.Cells[1].Border.Bottom.Width = 1;
row1.Cells[1].Border.Color = RgbColor.Orange;
row1.Cells.Add("A3");
row1.Cells[1].ColumnSpan = 2;

Row2 row3 = table.Rows.Add(40);
row3.Cells.Add("c2");
row3.Cells.Add("c3");
//set background color to light blue and set lines as dashed, but do not display the
//top line
row3.Cells[0].BackgroundColor = RgbColor.LightBlue;
row3.Cells[0].Border.LineStyle = LineStyle.DashSmall;
row3.Cells[0].Border.Top.Width = 0;

//do not display the top or left line
row3.Cells[1].Border.Top.Width = 0;
row3.Cells[1].Border.Left.Width = 0;

page.Elements.Add(table);

Creating a Table

To create a table that appears on a PDF, instantiate a Table2 class instance. Specify the Table2 instance's Height and Width and its X and Y coordinates. The x and y coordinates position the table's top left corner on the page at those coordinates. The table's height and width are the vertical or horizontal space allotted.

It is optional that a table's final height and width are known at design-time. A table and its sizes are often dynamic and can be determined from dynamically generated data at runtime. Although instantiating a Table2 instance requires setting the table's x, y, width, and height in its constructor, these values can be modified later.

One possible strategy is setting the height and width to the largest possible value a page can accommodate and later replacing the values if the table has less content. In addition to the x, y, height, and width, any formatting that has to be the default for the entire table should be specified here.

Next, define the table's column widths, and remember once the column width is set, it is read-only and can not be changed.

After defining the columns, create the table. Start by adding a row to the table and then adding cells to that row. Repeat as necessary. Also, apply any formatting required to a table.

Add rows to the table, and a particular height for the row can also be set. The row heights will be the minimum height; if needed, the height will automatically expand to fit the largest element (either text or any page element that implements IArea) in that row. Note the third row in the example below.

// Create a PDF Document 
Document document = new Document();

// Create a Page and add it to the document 
Page page = new Page();
document.Pages.Add(page);

Table2 table = new Table2(0, 0, 600, 600);
table.CellDefault.Border.Color = RgbColor.Blue;
table.CellSpacing = 5.0f;

// Add columns to the table
table.Columns.Add(150);
table.Columns.Add(90);
table.Columns.Add(90);

// Add rows to the table and add cells to the rows
Row2 row1 = table.Rows.Add(40, Font.HelveticaBold, 16, RgbColor.Black,
    RgbColor.Gray);
row1.CellDefault.Align = TextAlign.Center;
row1.CellDefault.VAlign = VAlign.Center;
row1.Cells.Add("Header 1");
row1.Cells.Add("Header 2");
row1.Cells.Add("Header 3");

Row2 row2 = table.Rows.Add(30);
Cell2 cell1 = row2.Cells.Add("Rowheader 1", Font.HelveticaBold, 16,
    RgbColor.Black, RgbColor.Gray, 1);
cell1.Align = TextAlign.Center;
cell1.VAlign = VAlign.Center;
row2.Cells.Add("Item 1");
row2.Cells.Add("Item 2, this item is much longer than the rest so that " +
    "you can see that each row will automatically expand to fit to the " +
    "height of the largest element in that row.");

Row2 row3 = table.Rows.Add(30);
Cell2 cell2 = row3.Cells.Add("Rowheader 2", Font.HelveticaBold, 16,
    RgbColor.Black, RgbColor.Gray, 1);
cell2.Align = TextAlign.Center;
cell2.VAlign = VAlign.Center;
row3.Cells.Add("Item 3");
row3.Cells.Add("Item 4");

// Add the table to the page
page.Elements.Add(table);
// Save the PDF 
document.Draw(pdfFilePath);           
' Create a PDF Document
Dim MyDocument As Document = New Document

' Create a Page and add it to the document
Dim MyPage As Page = New Page
MyDocument.Pages.Add(MyPage)

Dim MyTable As Table2 = New Table2(0, 0, 600, 600)
MyTable.CellDefault.Border.Color = RgbColor.Blue
MyTable.CellSpacing = 5.0F

' Add columns to the table
MyTable.Columns.Add(150)
MyTable.Columns.Add(90)
MyTable.Columns.Add(90)

' Add rows to the table and add cells to the rows
Dim MyRow1 As Row2 = MyTable.Rows.Add(40, ceTe.DynamicPDF.Font.HelveticaBold, \_
    16, RgbColor.Black, RgbColor.Gray)
MyRow1.CellDefault.Align = TextAlign.Center
MyRow1.CellDefault.VAlign = VAlign.Center
MyRow1.Cells.Add("Header 1")
MyRow1.Cells.Add("Header 2")
MyRow1.Cells.Add("Header 3")
Dim MyRow2 As Row2 = MyTable.Rows.Add(30)
Dim MyCell1 As Cell2 = MyRow2.Cells.Add("Rowheader 1", Font.HelveticaBold, 16, \_
    RgbColor.Black, RgbColor.Gray, 1)
MyCell1.Align = TextAlign.Center
MyCell1.VAlign = VAlign.Center
MyRow2.Cells.Add("Item 1")
MyRow2.Cells.Add("Item 2, this item is much longer than the rest so that you " + \_
    "can see that each row will automatically expand to fit to the height of " + \_
    "the largest element in that row.")
Dim MyRow3 As Row2 = MyTable.Rows.Add(30)
Dim MyCell2 As Cell2 = MyRow3.Cells.Add("Rowheader 2", Font.HelveticaBold, 16, \_
    RgbColor.Black, RgbColor.Gray, 1)
MyCell2.Align = TextAlign.Center
MyCell2.VAlign = VAlign.Center
MyRow3.Cells.Add("Item 3")
MyRow3.Cells.Add("Item 4")

' Add the table to the page
MyPage.Elements.Add(MyTable)

' Save the PDF
MyDocument.Draw(pdfFilePath)      

In this topic