Adding Page Elements to Cells

Add any page element (Page Elements) to a table cell. Page elements that implement IArea (Rectangle, Image, Label, etc.) are added directly to a table cell. Elements not implementing IArea are added to a table cell by first adding them to an AreaGroup. The examples below illustrate both techniques.

If unfamiliar with using tables, refer to the Tables documentation topic, which demonstrates creating a simple table using strings of text for each cell.

Adding IArea Elements

Page elements that implement IArea allow adding directly to a cell in the row. You can add any IArea subclass because it supports defining height and width and can be visually arranged in a table cell.

Page Elements that implement IARea can be added directly to a table cell.

Adding Non-IArea Elements

If a page element does not implement IArea, then before adding that page element to the table, first add it to an AreaGroup. The area group allows setting height and width to page elements that do not define height and width. The fourth row on the example code illustrates adding two line page elements to an area group and adding the area group to a cell in the row.

Page Elements not implementing IArea must be added to an AreaGroup which is added to a table cell.

Adding Multiple Page Elements

An AreaGroup is also used to add multiple page elements to a cell. Simply define an AreaGroup, add all elements to that group, and then add the group to the cell.

Example Code

The following example illustrates adding page elements to a cell both directly and through an AreaGroup.

// 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);

// create a table
Table2 table = new Table2(0, 0, 400, 400);
table.CellDefault.Border.Color = RgbColor.Blue;
table.CellDefault.Border.LineStyle = LineStyle.Solid;
table.CellDefault.Padding.Value = 3.0f;

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

// The first row is used as the table heading
Row2 row1 = table.Rows.Add(20, Font.HelveticaBold, 14, RgbColor.Black,
RgbColor.Gray);
row1.CellDefault.Align = TextAlign.Center;
row1.CellDefault.VAlign = VAlign.Center;            
row1.Cells.Add("Page Element", 3);

// The second row is the column headings
Row2 row2 = table.Rows.Add(Font.HelveticaBoldOblique, 12);
row2.CellDefault.Align = TextAlign.Center;            
row2.Cells.Add("Name");
row2.Cells.Add("Element");
row2.Cells.Add("Implements IArea");

// Add IArea page elements directly to the cell
Row2 row3 = table.Rows.Add(30);
row3.Cells.Add("FormattedTextArea");
row3.CellDefault.Border.Color = RgbColor.Green;
string formattedText = "<font face="+"Times"+">font face,</font> <font size="+"0/>color,</font> <b>bold.</b>";
row3.Cells.Add(new FormattedTextArea(formattedText, 0, 0, 140, 50,FontFamily.Helvetica, 12, false));
Cell2 cell1 = row3.Cells.Add("YES");
cell1.Align = TextAlign.Center;
cell1.VAlign = VAlign.Center;

// Add page elemnts (Lines) to a cell using the AreaGroup
Row2 row4 = table.Rows.Add(30);
row4.Cells.Add("Line");
AreaGroup lineGroup = new AreaGroup(150, 150);
lineGroup.Add(new Line(25, 25, 125, 125, 5));
lineGroup.Add(new Line(25, 125, 125, 25, 5));
row4.Cells.Add(lineGroup);
Cell2 cell2 = row4.Cells.Add("NO");
cell2.Align = TextAlign.Center;
cell2.VAlign = VAlign.Center;

// 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, 400, 400)
MyTable.CellDefault.Border.Color = RgbColor.Blue
MyTable.CellDefault.Border.LineStyle = LineStyle.Solid
MyTable.CellDefault.Padding.Value = 3.0F

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

' The first row is used as a table heading
Dim MyRow1 As Row2 = MyTable.Rows.Add(20, Font.HelveticaBold, 14, RgbColor.Black, \_
            RgbColor.Gray)
MyRow1.CellDefault.Align = TextAlign.Center
MyRow1.CellDefault.VAlign = VAlign.Center
MyRow1.Cells.Add("Page Element", 3)

' The second row is the column headings
Dim MyRow2 As Row2 = MyTable.Rows.Add(Font.HelveticaBoldOblique, 12)
MyRow2.CellDefault.Align = TextAlign.Center
MyRow2.Cells.Add("Name")
MyRow2.Cells.Add("Element")
MyRow2.Cells.Add("Implements IArea")

' Add IArea page elements directly to the cell
Dim MyRow3 As Row2 = MyTable.Rows.Add(30)
MyRow3.Cells.Add("FormattedTextArea")
MyRow3.CellDefault.Border.Color = RgbColor.Green
Dim FormattedText As String = "<font face=" + "Times" + ">font face,</font> <font size=" + "0/>color,</font> <b>bold.</b>" 
MyRow3.Cells.Add(New FormattedTextArea(FormattedText, 0, 0, 140, 100,FontFamily.Helvetica, 12, False))
Dim MyCell1 As Cell2 = MyRow3.Cells.Add("YES")
MyCell1.Align = TextAlign.Center
MyCell1.VAlign = VAlign.Center

' Add page elements (Lines) to a cell using the AreaGroup
Dim MyRow4 As Row2 = MyTable.Rows.Add(30)
MyRow4.Cells.Add("Line")
Dim LineAreaGroup As AreaGroup = New AreaGroup(150, 150)
LineAreaGroup.Add(New Line(25, 25, 125, 125, 5))
LineAreaGroup.Add(New Line(25, 125, 125, 25, 5))
MyRow4.Cells.Add(LineAreaGroup)
Dim MyCell2 As Cell2 = MyRow4.Cells.Add("NO")
MyCell2.Align = TextAlign.Center
MyCell2.VAlign = VAlign.Center

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

' Save the PDF
MyDocument.Draw(pdfFilePath)

In this topic