Tables

A table object is used to format content within a PDF document and allows for control of the flow of data between sections or pages within that PDF (a Table Continuation topic follows). A table consists of one or more columns and one or more rows. Each row contains one or more cells. A cell can contain plain text or any page element (we will look at adding other page elements to the table in the Adding Page Elements to Cells topic). Formatting of the text (background color, font, font size, text color and alignment) in each cell is handled in a hierarchical manner. Specifically, the formatting specified within a cell takes precedence over the formatting specified within a row which in turn takes precedence over the formatting specified within a table. For example, if the entire table contains a certain format, then all the formatting information can be set on the table and every cell in every row will contain the appropriate formatting. If a row contains some formatting different than the rest of the table then the text format can be set on the row and every cell in that row will contain the particular formatting. Finally, if a cell contains a different formatting than the rest of the row then the formatting can be specified on the individual cell. Understanding this hierarchy will help to easily format any cell within the table as needed. Note that the Column class contains no formatting information.

By Default the Cell2 & Table2 border width is 1, hence a table created with default values will appear to have a table border width of 2. To achieve a border around all cells and around the entire table of 1, the Table's border width should be set to .5 and the Cell's default border width should be set to .5.

Simple Example

To create a table that appears on a PDF, first instantiate a Table class. Always specify the Height and Width as well as the X and Y coordinates where the table will be placed on the page (the X and Y coordinates is where the top left corner of the Table will be positioned on the page). The height and width specified will be the amount of vertical or horizontal space allotted for the table on a particular page. There is no need to try and guess the height and width of the final table during the instantiation of the Table object (since the table can be based on dynamic data and height or width may not always be known). While these height and width values are required by the Table's constructor, they can be reset once the Table is filled in. One strategy may be to set the height and width values to the biggest possible values that the page can accommodate and then go back and re-set them if the table's content is smaller. 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 width of the columns for the table and remember, once column width is set, it is read-only and can not be changed.

Once the columns are defined, the table is populated with data. This is done firstly, by adding a row to the table and then adding cells to that row. It is possible to add fewer cells to the row than the existing number of columns in the table, but it is not possible to add more cells than the number of columns. Cells can also span multiple columns. The key here is that the sum of all the column spans for all the cells in a row must be less than or equal to the total number of columns in that table.

Add rows to the table and a particular height for the row can also be set. This height will be the minimum height and if needed, the height will automatically expand to fit the largest element (either text or any page element that implements IArea) in that row. Take a look at 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