Creating table is easy in C# using itextSharp and if you are not aware of itextSharp and how to generate PDF using C#, please click here for more information.
Creating Table
Table can be created by creating object of PdfPTable Class. This class takes a parameter that refers to the number of columns required to be created in the table.
- PdfPTable table = new PdfPTable(3);
Next we need to add rows and columns to the above table object. This can be added by using the AddCell method of the above table object.
- table.AddCell("Row 1, Col 1");
- table.AddCell("Row 1, Col 2");
- table.AddCell("Row 1, Col 3");
- table.AddCell("Row 2, Col 1");
- table.AddCell("Row 2, Col 2");
- table.AddCell("Row 2, Col 3");
- table.AddCell("Row 3, Col 1");
- table.AddCell("Row 3, Col 2");
- table.AddCell("Row 3, Col 3");
- document.Add(table);

We have many techniques to send the data into the table cell but here, I am using phrase for text value to the table cell and also we can give many properties to the table cell like HorizontalAlignment, VerticalAlignment, Padding,Width etc. We use PdfPCell object for the table cell for assigning any properties.
Colspan in itextsharp using C#
itextsharp dll provides Colspan property to merge the columns in a table.
We use Colspan property on the PdfPCell object which creates the table cell. This object accepts many properties like ColSpan. After applying the required properties the created cell can be added to the table.
- PdfPTable table = new PdfPTable(3);
- PdfPCell cell = new PdfPCell(new Phrase("Row 1 , Col 1, Col 2 and col 3"));
- cell.Colspan = 3;
- cell.HorizontalAlignment = Element.ALIGN_CENTER;
- table.AddCell(cell);
- table.AddCell("Row 2, Col 1");
- table.AddCell("Row 2, Col 1");
- table.AddCell("Row 2, Col 1");
- table.AddCell("Row 3, Col 1");
- cell = new PdfPCell(new Phrase("Row 3, Col 2 and Col3"));
- cell.Colspan = 2;
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 4, Col 1 and Col2"));
- cell.Colspan = 2;
- table.AddCell(cell);
- table.AddCell("Row 4, Col 3");
- document.Add(table);

Rowspan in itextsharp using C#
It is similar to colspan and in the same way, we can use rowspan also in our code:
- PdfPTable table = new PdfPTable(3);
- PdfPCell cell = new PdfPCell(new Phrase("Row 1, Col 1"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 1, Col 2"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 1, Col 3"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 2 , Col 1"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 2, Col 2"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 2 and Row 3, Col 3"));
- cell.Rowspan = 2;
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 3, Col 1"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 3, Col 2"));
- table.AddCell(cell);
- document.Add(table);

Colspan and rowspan in itextsharp using C#
We can also use both colspan and rowspan for one table cell as below:
- PdfPTable table = new PdfPTable(3);
- PdfPCell cell = new PdfPCell(new Phrase("Row 1, Col 1"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 1, Col 2"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 1, Col 3"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 2 ,Col 1"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 2 and row 3, Col 2 and Col 3"));
- cell.Rowspan = 2;
- cell.Colspan = 2;
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 3, Col 1"));
- table.AddCell(cell);
- document.Add(table);

Rotate table cell in itextSharp using C#
We can also rotate a text inside a table cell but we can set the rotation property as multiple of 90 only as below.
- PdfPTable table = new PdfPTable(3);
- PdfPCell cell = new PdfPCell(new Phrase("Row 1, Col 1"));
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 1, Col 2"));
- cell.Rotation = 90;
- table.AddCell(cell);
- cell = new PdfPCell(new Phrase("Row 1, Col 3"));
- cell.Rotation = -90;
- table.AddCell(cell);
- document.Add(table);

In this way we can use Colspan, Rowspan and Rotation in the table in itextSharp using C#.
You can also download the sample code for your reference.

Zeb deePosted Mar 18, 2021, 10:53 AM
Hi Can anyone Please help me with the C# program to display AA,AB,AC..... so on for total rows/Record in PDFfile and it has to work as index, for eg:- 1stRow AA, 2ndRow AB, 3rdRow AC, 4thRow AD.........so on, Can any one please help me on this i am need in this code urgently, Thanks in advance, Zeb.
jariya boonchalewPosted Mar 3, 2020, 2:36 AM
Thank you so much
Sam HobbsPosted Jan 18, 2019, 6:21 PM
Is it possible to read tables in existing documents?
Siva kumarPosted Dec 26, 2018, 12:12 AM
Thanks, This helped me lot
Luiz JuniorPosted Jul 10, 2018, 8:54 AM
I need a table like this. carr | 01/07/2018 | 02/07/2018 | 03/07/2018 | | AP | OP | AP | OP | AP | OP | 1325 | X | | X | | | |
Luiz JuniorPosted Jul 10, 2018, 8:50 AM
I need a table like this.
Luiz JuniorPosted Jul 10, 2018, 8:49 AM
I need a table like this.
Amin MerchantPosted Apr 11, 2018, 12:20 AM
How to export database data into pdf using itextsharp in c# windows form
Shovan SahaPosted Aug 26, 2017, 12:33 PM
Dear Vivek Sir, How can I use "SutonnyMJ" and "Arial" font to make pdf from datagridview?
weng tanPosted Feb 21, 2017, 11:33 PM
What if the column is 10 20 or 30 are you going to repeat all these ?
Pruthvi PatelPosted Dec 21, 2016, 6:13 AM
If no of column is more than it is not fit in page and not visible So what to do in this scenario?
SubashPosted Aug 29, 2016, 5:28 AM
Great
Ranjeet PatraPosted Feb 6, 2016, 11:21 AM
Nice information...adds value to your previous article.