Insert table to PDF in C#.NET - Add Table to PDF SDK
Error processing SSI file

How to Add Table to PDF in C#.NET Code


Create a table and add it to PDF document is easy using iDiTect .NET SDK.

How to Create Simple PDF Table in C# language

This C# example is showing how to make a standard table format in PDF.

First, define the color and border style used in table.

Second, add table title. It can be simple text description, or image graphics.

Third, add table header. In this sample, we add three columns in the table. Each table column can be filled with text format, or some other format, like image, shape, or link.

Fourth, add table rows with data value.

Table table = new Table();

RgbColor bordersColor = new RgbColor(73, 90, 128);
RgbColor headerColor = new RgbColor(34, 143, 189);
RgbColor defaultRowColor = new RgbColor(176, 224, 230);

Border border = new Border(1, BorderStyle.Single, bordersColor);
table.Borders = new TableBorders(border);
table.DefaultCellState.Borders = new TableCellBorders(border, border, border, border);
table.DefaultCellState.Padding = new System.Windows.Thickness(2);

//Add table title
TableRow titleRow = table.Rows.AddTableRow();
TableCell titleCell = titleRow.Cells.AddTableCell();
titleCell.ColumnSpan = 3;
Block titleBlock = titleCell.Blocks.AddBlock();
titleBlock.HorizontalAlignment = HorizontalAlignment.Center;
titleBlock.InsertText("Simple Table Title");

//Add table header
TableRow headerRow = table.Rows.AddTableRow();
//Add first column
TableCell column1 = headerRow.Cells.AddTableCell();
column1.Background = headerColor;
column1.Borders = new TableCellBorders(border, border, border, border, null, border);
//Add second column
TableCell column2 = headerRow.Cells.AddTableCell();
column2.Background = headerColor;
Block column2Block = column2.Blocks.AddBlock();
column2Block.GraphicState.FillColor = RgbColors.White;
column2Block.InsertText("Product");
//Add third column
TableCell column3 = headerRow.Cells.AddTableCell();
column3.Background = headerColor;
Block column3Block = column3.Blocks.AddBlock();
column3Block.GraphicState.FillColor = RgbColors.White;
column3Block.InsertText("Price");
        
//Add table rows
Random r = new Random();
for (int i = 0; i < 50; i++)
{
    RgbColor rowColor = i % 2 == 0 ? defaultRowColor : RgbColors.White;

    TableRow row = table.Rows.AddTableRow();

    TableCell idCell = row.Cells.AddTableCell();
    idCell.Background = rowColor;
    idCell.Blocks.AddBlock().InsertText(i.ToString());

    TableCell productCell = row.Cells.AddTableCell();
    productCell.Background = rowColor;
    productCell.Blocks.AddBlock().InsertText(String.Format("Product{0}", i));

    TableCell priceCell = row.Cells.AddTableCell();
    priceCell.Background = rowColor;
    priceCell.Blocks.AddBlock().InsertText(r.Next(10, 1000).ToString()); 
}

return table;

The screenshot of the simple table in document.

How to Create Table Frame in PDF in C# language

This C# example is showing how to add table layout to PDF.

You can keep the table border, it's more clearly to show the table frame. Or you can hide the table border to make it looks more "clean".

Merged cells in table is also supported, so it's more flexible uesd in real world.

Table table = new Table();

//Set table border
table.Margin = new System.Windows.Thickness(3);
table.Borders = new TableBorders(new Border(3, RgbColors.Black));
table.BorderSpacing = 5;
//Set cell border
Border border = new Border(1, RgbColors.Black);
table.DefaultCellState.Borders = new TableCellBorders(border, border, border, border);
table.DefaultCellState.Padding = new System.Windows.Thickness(5);

TableRow row = table.Rows.AddTableRow();

//Add a merged cell in 2x2
TableCell cell = row.Cells.AddTableCell();
cell.RowSpan = 2;
cell.ColumnSpan = 2;
cell.Blocks.AddBlock().InsertText("Text 1");

//Add a single cell
cell = row.Cells.AddTableCell();
cell.Blocks.AddBlock().InsertText("Text 2");

row = table.Rows.AddTableRow();
//Add a single cell
cell = row.Cells.AddTableCell();
cell.Blocks.AddBlock().InsertText("Text 3");

row = table.Rows.AddTableRow();
//Add a single cell
cell = row.Cells.AddTableCell();
cell.Blocks.AddBlock().InsertText("Text 4");

//Add a merged cell in 1x2
cell = row.Cells.AddTableCell();
cell.ColumnSpan = 2;
cell.Blocks.AddBlock().InsertText("Text 5");

return table;

The screenshot of the table frame in document.

What iDiTect .NET Document component can do

We provide powerful & profession document & image controls: Read excel file row by row C# how to insert Excel row or rows in C#, VB.NET via this Excel component. Simple PDF to Image Conversiont save PDF page to high quality image, converting PDF to compressed jpg and multipage tiff image in C# language. Create password protected PDF using C# and VB.Net help to create PDF using Acrobat objects and secure the file by setting password and encryption programmatically. C# - Add table into existing PDF you may easily create new pdf document with table. C#: insert text to an existing pdf Best and multifunctional Visual Studio .NET PDF SDK supports adding and inserting text content to adobe PDF document in C#. Read and Extract PDF Text from C# / VB.NET applications how to extract all text from PDF file into TXT file (plain text) using PDF Extractor SDK.

More Programming Tutorials
More Programming FAQs