Available for: UI for ASP.NET AJAX | UI for ASP.NET MVC | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin


The Table class helps you easily create tabular data content. All you need to do is define the table content and pass a Table instance to a FixedContentEditor or a RadFixedDocumentEditor. From then on, these editors are responsible for positioning, measuring, drawing and splitting the table onto pages. Rad Pdf Processing Editing Table 01

This article aims to present the table-related API in RadPdfProcessing. It contains the following sections:

Defining Table Content

Each table contains a series of TableRow instances each of which contains a series of TableCell instances. In order to define a simple table, you need to generate the table cells and add some content to them.

Example 1 shows how to generate a simple table with two rows and three columns with some sample text in each table cell.

Example 1: Create simple table

Table table = new Table(); 
TableRow firstRow = table.Rows.AddTableRow(); 
TableRow secondRow = table.Rows.AddTableRow(); 

The result table is shown in Figure 1.

Figure 1: Table

Rad Pdf Processing Editing Table 02

Using DefaultCellProperties

If you want to apply default styling to all the cells in a table, you can use Table's DefaultCellProperties property. This allows to easily modify the default cell presentation.

  • Padding: Specifies the distances between the inner cell border contour and the cell content.
  • Borders: Property of type TableCellBorders, which specifies the borders of a single cell. The available borders are left, right, top, bottom, diagonal up and diagonal down.
  • Background: Specifies the background of the cell.

Еxample 2 shows how to use the DefaultCellProperties of a table

Example 2: Use DefaultCellProperties of Table

Table table = new Table(); 
Border redBorder = new Border(2, new RgbColor(255, 0, 0)); 
table.DefaultCellProperties.Borders = new TableCellBorders(redBorder); 
table.DefaultCellProperties.Padding = new Thickness(20, 10, 20, 10); 
table.DefaultCellProperties.Background = new RgbColor(0, 255, 0); 
TableRow firstRow = table.Rows.AddTableRow(); 
TableRow secondRow = table.Rows.AddTableRow(); 

The result of the snippet in Example 2 is demonstrated on Figure 2.

Figure 2: Result of DefaultCellProperties modification

Rad Pdf Processing Editing Table 03

Modifying a Table

There are several factors that affect tables measuring calculations. Some of them are listed and explained bellow:

  • Margin: Specifies the distances between the table borders outline and the rest of the document's content.

  • Padding: Set through the TableCell's Padding property, it specifies the distances between cell borders inner contour and the cell content.

  • LayoutType: Specifies the algorithm, which shall be used to layout table contents. There are two options available in the TableLayoutType enumeration:

    • AutoFit – The table width fits the content unless the needed width is bigger than the available measuring width.
    • FixedWidth – The table width always fits the available measuring width.
  • BorderSpacing: Specifies the distance between all the borders in the table. This distance is measured differently depending on the BorderCollapse option.

  • BorderCollapse: Specifies the way the border spacing calculations should be done. There are two options:

    • Collapse: The distance between borders is measured from the middle lines of the borders
    • Separate: The distance between borders is measured from the outer border contour.

Example 3 demonstrates how border calculations occur with different BorderCollapse option. The code in this example creates an empty table and sets default cell padding and red table border with thickness 10 to it.

Example 3: Create table with red border

Table table = new Table(); 
table.DefaultCellProperties.Padding = new Thickness(10, 6, 10, 6); 
Border redBorder = new Border(10, new RgbColor(255, 0, 0)); 
table.Borders = new TableBorders(redBorder); 

Example 4 adds a single row with two cells to the table from Example 3. The first cell has a green border with thickness 5 while the second cell has a blue border with thickness 3.

Example 4: Add green and blue cells

TableRow tableRow = table.Rows.AddTableRow(); 
TableCell firstCell = tableRow.Cells.AddTableCell(); 
Border greenBorder = new Border(5, new RgbColor(0, 255, 0)); 
firstCell.Borders = new TableCellBorders(greenBorder, greenBorder, greenBorder, greenBorder); 
firstCell.Blocks.AddBlock().InsertText("green bordered cell"); 
TableCell secondCell = tableRow.Cells.AddTableCell(); 
Border blueBorder = new Border(3, new RgbColor(0, 0, 255)); 
secondCell.Borders = new TableCellBorders(blueBorder, blueBorder, blueBorder, blueBorder); 
secondCell.Blocks.AddBlock().InsertText("blue bordered cell"); 

Figure 3 shows the table from Example 3 and 4 with BorderCollapse property set to Collapse - all borders are drawn so that their middle lines coincide.

Example 5: Collapse border

table.BorderCollapse = BorderCollapse.Collapse; 

Figure 3: Collapsed border

Rad Pdf Processing Editing Table 04

Figure 4 shows the same table with BorderCollapse property set to Separate - all borders are drawn so that their outer contour coincide.

Example 6: Separate border

table.BorderCollapse = BorderCollapse.Separate; 

Figure 4: Separated border

Rad Pdf Processing Editing Table 05

Drawing Table with RadFixedDocumentEditor

When a table is generated, it could be inserted in the PDF document using the RadFixedDocumentEditor's InsertTable() method. This way the table is inserted in the document and split onto pages if necessary.

Example 7 generates a simple table with two cells.

Example 7: Create table

Table table = new Table(); 
Border border = new Border(); 
table.Borders = new TableBorders(border); 
table.DefaultCellProperties.Borders = new TableCellBorders(border, border, border, border); 
table.BorderSpacing = 5; 
table.BorderCollapse = BorderCollapse.Separate; 
TableRow row = table.Rows.AddTableRow(); 
row.Cells.AddTableCell().Blocks.AddBlock().InsertText("First cell"); 
row.Cells.AddTableCell().Blocks.AddBlock().InsertText("Second cell"); 

Example 8 inserts the table from Example 7 in a RadFixedDocumentEditor and specifies the table layout type to AutoFit.

Example 8: Insert AutoFit table

RadFixedDocumentEditor editor = new RadFixedDocumentEditor(document); 
table.LayoutType = TableLayoutType.AutoFit; 

The result is that the table width is exactly as needed for fitting the cells content as visible in Figure 5.

Figure 5: AutoFit table

Rad Pdf Processing Editing Table 06

Specifying FixedWidth layout option produces different results.

Example 9: Insert FixedWidth table

table.LayoutType = TableLayoutType.FixedWidth; 

Figure 6: FixedWidth table

Rad Pdf Processing Editing Table 07

Drawing Table with FixedContentEditor

When in need of more customization options, you can use the DrawTable() method of FixedContentEditor instead of RadFixedDocumentEditor.

Example 10 shows how to draw a rotated table with the help of FixedContentEditor.

Example 10: Draw rotated table

Table table = GenerateSampleTable(); 
RadFixedDocument document = new RadFixedDocument(); 
RadFixedPage page = document.Pages.AddPage(); 
FixedContentEditor editor = new FixedContentEditor(page, new SimplePosition()); 
editor.Position.Translate(10, 100); 

As a result, on Figure 7 you can see a 45-degree rotated table similar to the one on Figure 5.

Figure 7: FixedWidth table

Rad Pdf Processing Editing Table 08

See Also

In this article
Not finding the help you need? Improve this article