Cellbi.DocFramework Tables Quick Start
Sample code language:
Simple Table
Cellbi.DocFramework table model allows easy creation of document tables.
It is very flexible and provides table rows, columns, cells, cells merging and so on.
Table model consists mainly of these classes: Table, TableColumn, TableCell and TableRow.
In this article we'll add a simple table to our document, here is how it would look like:
Table class can be used to define a table in a document.
Here is an example code to create a table with 2 rows and 3 columns:
Document doc = new Document();
Table table = doc.AppendTable(2, 3);
The above code will create a table with 2 rows and 3 columns and without any borders.
Let's add table borders:
Border b = new Border();
b.BorderType = BorderType.Single;
TableBorders borders = table.Borders;
borders.Left = b;
borders.Right = b;
borders.Top = b;
borders.Bottom = b;
borders.InnerHorizontal = b;
borders.InnerVertical = b;
And some text to the table cells:
table[0, 0].Text = "Name";
table[0, 1].Text = "Price";
table[0, 2].Text = "Quantity";
table[1, 0].Text = "MyProduct";
table[1, 1].Text = "10";
table[1, 2].Text = "1";
So far we have created two rows in the table, let's change first row formatting in the table.
We'll set bold text for each cell, add light gray background and center text in each cell.
Here is the sample code:
TableRow firstRow = table.FirstRow;
firstRow.Font.Bold = true;
ITableCellList rowCells = firstRow.GetCells();
rowCells.CellShading.BackgroundColor = ColorCode.LtGray;
foreach (TableCell cell in rowCells)
cell.GetParagraphs().TextAlignment = TextAlignment.Centered;
Finally we'll center text in the second row:
ITableRowList rows = table.GetRows();
TableRow secondRow = rows[1];
foreach (TableCell cell in secondRow.GetCells())
cell.GetParagraphs().TextAlignment = TextAlignment.Centered;