Create Table In PDF using C# And iTextSharp

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.

  1. PdfPTable table = new PdfPTable(3);  
This creates the table with 3 columns.

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.
  1. table.AddCell("Row 1, Col 1");  
  2. table.AddCell("Row 1, Col 2");  
  3. table.AddCell("Row 1, Col 3");  
  4.   
  5. table.AddCell("Row 2, Col 1");  
  6. table.AddCell("Row 2, Col 2");  
  7. table.AddCell("Row 2, Col 3");  
  8.   
  9. table.AddCell("Row 3, Col 1");  
  10. table.AddCell("Row 3, Col 2");  
  11. table.AddCell("Row 3, Col 3");  
Now we have to add this table object to the document (pdf) that we are creating by using the following line of code.
  1. document.Add(table);  
Now we have created a table with data as below.
see result

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.
  1. PdfPTable table = new PdfPTable(3);  
  2.   
  3. PdfPCell cell = new PdfPCell(new Phrase("Row 1 , Col 1, Col 2 and col 3"));  
  4. cell.Colspan = 3;  
  5. cell.HorizontalAlignment = Element.ALIGN_CENTER;  
  6. table.AddCell(cell);  
  7.   
  8. table.AddCell("Row 2, Col 1");  
  9. table.AddCell("Row 2, Col 1");  
  10. table.AddCell("Row 2, Col 1");  
  11.   
  12. table.AddCell("Row 3, Col 1");  
  13. cell = new PdfPCell(new Phrase("Row 3, Col 2 and Col3"));  
  14. cell.Colspan = 2;  
  15. table.AddCell(cell);  
  16.   
  17. cell = new PdfPCell(new Phrase("Row 4, Col 1 and Col2"));  
  18. cell.Colspan = 2;  
  19. table.AddCell(cell);  
  20. table.AddCell("Row 4, Col 3");  
  21.   
  22. document.Add(table);  
Output:

see output

Rowspan in itextsharp using C#

It is similar to colspan and in the same way, we can use rowspan also in our code:
  1. PdfPTable table = new PdfPTable(3);  
  2.   
  3. PdfPCell cell = new PdfPCell(new Phrase("Row 1, Col 1"));  
  4. table.AddCell(cell);  
  5. cell = new PdfPCell(new Phrase("Row 1, Col 2"));  
  6. table.AddCell(cell);  
  7. cell = new PdfPCell(new Phrase("Row 1, Col 3"));  
  8. table.AddCell(cell);  
  9.   
  10. cell = new PdfPCell(new Phrase("Row 2 , Col 1"));  
  11. table.AddCell(cell);  
  12. cell = new PdfPCell(new Phrase("Row 2, Col 2"));  
  13. table.AddCell(cell);  
  14. cell = new PdfPCell(new Phrase("Row 2 and Row 3, Col 3"));  
  15. cell.Rowspan = 2;  
  16. table.AddCell(cell);  
  17.   
  18. cell = new PdfPCell(new Phrase("Row 3, Col 1"));  
  19. table.AddCell(cell);  
  20. cell = new PdfPCell(new Phrase("Row 3, Col 2"));  
  21. table.AddCell(cell);  
  22.   
  23. document.Add(table);  
Output:

run

Colspan and rowspan in itextsharp using C#

We can also use both colspan and rowspan for one table cell as below:
  1. PdfPTable table = new PdfPTable(3);  
  2.   
  3. PdfPCell cell = new PdfPCell(new Phrase("Row 1, Col 1"));  
  4. table.AddCell(cell);  
  5. cell = new PdfPCell(new Phrase("Row 1, Col 2"));  
  6. table.AddCell(cell);  
  7. cell = new PdfPCell(new Phrase("Row 1, Col 3"));  
  8. table.AddCell(cell);  
  9.   
  10. cell = new PdfPCell(new Phrase("Row 2 ,Col 1"));  
  11. table.AddCell(cell);  
  12.   
  13. cell = new PdfPCell(new Phrase("Row 2 and row 3, Col 2 and Col 3"));  
  14. cell.Rowspan = 2;  
  15. cell.Colspan = 2;  
  16. table.AddCell(cell);  
  17.   
  18. cell = new PdfPCell(new Phrase("Row 3, Col 1"));  
  19. table.AddCell(cell);  
  20.   
  21. document.Add(table);  
Output:

result

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.
  1. PdfPTable table = new PdfPTable(3);  
  2.   
  3. PdfPCell cell = new PdfPCell(new Phrase("Row 1, Col 1"));  
  4. table.AddCell(cell);  
  5.   
  6. cell = new PdfPCell(new Phrase("Row 1, Col 2"));  
  7. cell.Rotation = 90;  
  8. table.AddCell(cell);  
  9.   
  10. cell = new PdfPCell(new Phrase("Row 1, Col 3"));  
  11. cell.Rotation = -90;  
  12. table.AddCell(cell);  
  13.   
  14. document.Add(table);  
Output:

Output

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.