Adding Page Elements to Cells

You can add any page element to a table. Refer to the Tables topic, which demonstrates creating a simple table using strings of text for each cell. The Table2 object also allows adding any DynamicPDF Page Elements (including another Table2) into a cell in a table.

In the example include below, you add page elements to a table. First instantiate a table object, define the columns, and then start adding the rows and cells. The first two rows are used for headings. The next two rows of the table add page elements into a cell. The third row adds a FormattedTextArea page element that implements IArea, while the fourth row adds two Line page elements that do not implement IArea.

Adding IArea Elements

Some page elements implement IArea (Rectangle, Image, Label, etc..), while others do not (Line, Circle, etc.). The implementation of IArea allows a particular page element to have a defined height and width. If the particular page element added to a cell does implement IArea, then add that element directly to a cell in the row. Adding the FormattedTextArea on the third row of the example code illustrates.

Adding Non-IArea Elements

If the page element added to a table 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 on 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 then adding the area group to a cell in the row.

Adding Multiple Page Elements

An AreaGroup is used to add more than one page element to a cell. To add the AreaGroup, define an AreaGroup, add all elements to that group, and then add the group to the cell.

Example Code

The following examples illustrates adding page elements to a cell.

// 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, 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 a 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