Using iTextSharp to operate a table is simple. In particular, the naming of Table Elements in iTextSharp is very similar to that in HTML and CSS. ITextSharp provides multiple classes for creating tables. To avoid confusion, I use PdfPTable, a class dedicated to creating tables in PDF, the following code creates a table and adds it to a PDF file:
PdfPTable table = new PdfPTable (3 );
Export pcell cell = new export pcell (new Phrase ("Header spanning 3 columns "));
Cell. Colspan = 3;
Cell. HorizontalAlignment = 1; // 0 = Left, 1 = Centre, 2 = Right
Table. AddCell (cell );
Table. AddCell ("Col 1 Row 1 ");
Table. AddCell ("Col 2 Row 1 ");
Table. AddCell ("Col 3 Row 1 ");
Table. AddCell ("Col 1 Row 2 ");
Table. AddCell ("Col 2 Row 2 ");
Table. AddCell ("Col 3 Row 2 ");
Doc. Add (table );
Input an integer 3 for the pdfpTable constructor. The pdfpTable is initialized as a table with three columns. there are multiple ways to add cells to pdfpTabled. The first Cell is added through the plupcell object. The plupcell constructor accepts a Phrase object as a parameter and sets the colspan of the Cell to 3, in this way, the cell occupies the entire row. like tables in HTML, the horizontal alignment of cells uses one of the three values (TRANSLATOR: Left alignment, center, right alignment). I added these three values to the comments. I add the following cells through the AddCell method. The final effect of the document is as follows:
The following code extracts values from the database and inserts the data into the table generated by iTextSharp. The following Code also sets the table display mode:
PdfPTable table = new PdfPTable (2 );
// Actual width of table in points
Table. TotalWidth = 216f;
// Fix the absolute width of the table
Table. LockedWidth = true;
// Relative col widths in proportions-1/3 and 2/3
Float [] widths = new float [] {1f, 2f };
Table. SetWidths (widths );
Table. HorizontalAlignment = 0;
// Leave a gap before and after the table
Table. SpacingBefore = 20f;
Table. SpacingAfter = 30f;
Export pcell cell = new export pcell (new Phrase ("Products "));
Cell. Colspan = 2;
Cell. Border = 0;
Cell. HorizontalAlignment = 1;
Table. AddCell (cell );
String connect = "Server =. \ SQLEXPRESS; Database = Northwind; Trusted_Connection = True ;";
Using (SqlConnection conn = new SqlConnection (connect ))
{
String query = "SELECT ProductID, ProductName FROM Products ";
SqlCommand cmd = new SqlCommand (query, conn );
Try
{
Conn. Open ();
Using (SqlDataReader rdr = cmd. ExecuteReader ())
{
While (rdr. Read ())
{
Table. AddCell (rdr [0]. ToString ());
Table. AddCell (rdr [1]. ToString ());
}
}
}
Catch (Exception ex)
{
Response. Write (ex. Message );
}
Doc. Add (table );
}
The table is initialized as a table with two columns at the beginning, and the fixed width of the table is set, and the relative width of each column is set to 1/3 and 2/3 of the entire table. If you want to set the width to and, you only need to change the parameters to 1f and 4f respectively. to set the absolute width of each column, you only need to input the column width and the total width of the table. For example:
Float [] widths = new float [] {100f, 0000f };
By setting the SpacingBefore and SpacingAfter attributes of a table, you can set the distance from the table header to the previous element and the distance from the end of the table to the next element. this function is particularly effective when there are several tables in the document that are close to each other. If you do not set the preceding attributes, the distance between tables is the same as the distance of a carriage return in word, which is as fine as the needle. Next, we set the border of the first cell to 0, colspan to the number of columns, and center it like the table title. Next, we add the data read from SqlDataReader to the cell dynamically by programming and finally add the table:
The following code shows some options for formatting cells. As you can see, the author of iTextSharp follows the CSS naming rules to set cell options to make it easier to format cells (of course, I suppose you understand CSS ...) :
PdfPTable table = new PdfPTable (3 );
Table. AddCell ("Cell 1 ");
Export pcell cell = new export pcell (new Phrase ("Cell 2", new Font (Font. HELVETICA, 8f, Font. NORMAL, Color. YELLOW )));
Cell. BackgroundColor = new Color (0,150, 0 );
Cell. BorderColor = new Color (255,242, 0 );
Cell. Border = Rectangle. BOTTOM_BORDER | Rectangle. TOP_BORDER;
Cell. BorderWidthBottom = 3f;
Cell. BorderWidthTop = 3f;
Cell. PaddingBottom = 10f;
Cell. PaddingLeft = 20f;
Cell. PaddingTop = 4f;
Table. AddCell (cell );
Table. AddCell ("Cell 3 ");
Doc. Add (table );
The code above makes it easy to set colspan to make a cell horizontally cross multiple rows. What if a cell spans multiple rows vertically? You can use the Rowspan attribute in HTML, but it does not have the Rowspan attribute in iTextSharp. Therefore, only nested tables can be used to achieve this goal. The following code creates a table with four columns. The table in the lower right corner spans three columns and vertical spans three rows. Of course, this looks like this on the surface, but it is actually done by embedding a table with three rows and one column in the cells in the lower left corner of the table, we set the padding of all cells in the nested subtable in the lower left to 0 so that the embedded subtable occupies the entire lower left cell:
PdfPTable table = new PdfPTable (3 );
Export pcell cell = new export pcell (new Phrase ("Header spanning 3 columns "));
Cell. Colspan = 3;
Cell. HorizontalAlignment = 1; // 0 = Left, 1 = Centre, 2 = Right
Table. AddCell (cell );
Table. AddCell ("Col 1 Row 1 ");
Table. AddCell ("Col 2 Row 1 ");
Table. AddCell ("Col 3 Row 1 ");
Table. AddCell ("Col 1 Row 2 ");
Table. AddCell ("Col 2 Row 2 ");
Table. AddCell ("Col 3 Row 2 ");
Doc. Add (table );
Finally, at the end of this article, we will take a look at how to rotate the text in a cell:
PdfPTable table = new PdfPTable (3 );
Table. TotalWidth = 144f;
Table. LockedWidth = true;
Table. HorizontalAlignment = 0;
Export pcell left = new export pcell (new Paragraph ("Rotated "));
Left. Rotation = 90;
Table. AddCell (left );
Export pcell middle = new export pcell (new Paragraph ("Rotated "));
Middle. Rotation =-90;
Table. AddCell (middle );
Table. AddCell ("Not Rotated ");
Doc. Add (table );
The Rotation attribute must be set to a multiple of 90. Otherwise, an error will occur. The Rotation of the middle cell is set to-90 and 270, and the result is the same. By default, this degree is calculated in a counter-clockwise manner:
In fact, iTextSharp is very powerful in Table operations. I will elaborate more in future articles. At the same time, you can use Visual Studio's smart perception and Object Browser to fully tap into the potential of iTextSharp and see how the final result is generated.
--------------------------------
Original article: iTextSharp-Introducing-Tables translated by CareySon
<Script> </script>