Skip to main content

Adding Page Elements to Cells

Version: 7.0

Adding Page Elements to Cells

The main Tables topic demonstrates how to create 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 below, it shows how to add any of our page elements to the table. In this example, instantiate a table object, define the columns and then start adding the rows and cells. The first two rows in the example below are used for headings. The next two rows of the table add page elements into a cell. Row 3 adds a FormattedTextArea page element that implements IArea, while Row 4 adds two Line page elements that do not implement IArea. Let's take a look at the differences.

Adding IArea Elements​

Some page elements implement IArea (Rectangle, Image, Label, etc.), while others do not (Line, Circle, etc.). The implementation of IArea simply means that the particular page element can have a defined height and width. If the particular page element that has to be added to the cell does implement IArea then add that element directly to a cell in the row. This is displayed by the Row 3 below with adding the FormattedTextArea.

Adding Non-IArea Elements​

If the page element that has to be added to the table does not implement IArea then before adding that page element to the table, first add it to an AreaGroup. The area group is designed to give the ability to set a height and width on page elements that do not have the height and width defined. This is all displayed in Row4 with the addition of two line page elements to an area group and then adding the area group to a cell in the row.

Adding Multiple Page Elements​

More than one page element can also be added to a cell. For this an AreaGroup would be used again. First define an AreaGroup, add all elements to that group and then add the group to the 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(@"C:\MyDocument.pdf");
' 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, color, **bold.**"
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 elemnts (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("C:\MyDocument.pdf")