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);
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.
- 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");
Now we have to add this table object to the document (pdf) that we are creating by using the following line of code.
Now we have created a table with data as below.
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);
Output:
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);
Output:
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);
Output:
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);
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.