Adding Page Elements to Table 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 image to table

Row2 row5 = table.Rows.Add(30);
row5.Cells.Add("Image");
Image img = new("DPDFLogo.png", 0,0);
row5.Cells.Add(img);
Cell2 cell3 = row5.Cells.Add("Yes");
cell3.Align = TextAlign.Center;
cell3.VAlign = VAlign.Center;

page.Elements.Add(table);
document.Draw(outputPath);       
Dim document As New Document()

Dim page As New Page()
document.Pages.Add(page)

Dim table As New Table2(0, 0, 400, 400)
table.CellDefault.Border.Color = RgbColor.Blue
table.CellDefault.Border.LineStyle = LineStyle.Solid
table.CellDefault.Padding.Value = 3.0F

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

Dim row1 As Row2 = 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)

Dim row2 As 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")

Dim row3 As Row2 = table.Rows.Add(30)
row3.Cells.Add("FormattedTextArea")
row3.CellDefault.Border.Color = RgbColor.Green

Dim formattedText As String = "<font face=" & "Times" & ">This is a formatted text area. 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))

Dim cell1 As Cell2 = row3.Cells.Add("YES")
cell1.Align = TextAlign.Center
cell1.VAlign = VAlign.Center

Dim row4 As Row2 = table.Rows.Add(30)
row4.Cells.Add("Line")
Dim lineGroup As 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)
Dim cell2 As Cell2 = row4.Cells.Add("NO")
cell2.Align = TextAlign.Center
cell2.VAlign = VAlign.Center

Dim row5 As Row2 = table.Rows.Add(30)
row5.Cells.Add("Image")
Dim img As New Image("DPDFLogo.png", 0, 0)
row5.Cells.Add(img)
Dim cell3 As Cell2 = row5.Cells.Add("YES")
cell3.Align = TextAlign.Center
cell3.VAlign = VAlign.Center

page.Elements.Add(table)

document.Draw(outputPath)

Formatted Table

Figure 1. Adding page elements to a table in a PDF document.

In this topic