ITextSharp 5 has canceled the Table class. I checked the help document of iTextSharp 4 and used the smart prompt of VS to find all the namespaces and could not find the Table class, fortunately, we finally saw a PdfPTable class, and it turned out to be a dead horse doctor. Therefore, in iTextSharp 5, the operation table can only be performed through PdfTable, the full name is iTextSharp.text.pdf. PdfPTable.
Compared with Word, iTextSharp seems clumsy in Table operations and does not seem to be capable of cross-row or cross-column cells. Therefore, to create a table of this type, you can only embed a table in the cell.
ITextSharp usually has two levels of operations on tables: tables --> cells. When creating a new table, you need to set the number of columns in the table, and then add existing cells by AddCell, the classes involved in this step include PdfPTable and javaspcell, which are located in the same namespace. The basic program is as follows:
Itextsharp.textdetail. PdfPTable table = new itextsharp.textdetail. PdfPTable (2); // The table has two columns.
Itextsharp.textalign. Adjust pcell cell = new itextsharp.textalign. Partition pcell (); // create a cell
Cell. AddElement (element); // Add data to the cell
Table. AddCell (cell); // Add cells to the table.
Note: There is a trap when adding cells to a table. If the number of added cells is not an integer multiple of the number of columns, an exception is thrown. When cells are added in a loop, this problem is often ignored.
If you want to split the preceding cell into two columns, You need to embed the cell into another table, as shown below:
Itextsharp.textmetadata. PdfPTable innerTable = new itextsharp.textmetadata. PdfPTable (2 );
ITextSharp.text.pdf. Export pcell innerCell1 = ...;
InnerTable. AddCell (innerCell1 );
ITextSharp.text.pdf. javaspcell innerCell2 = ...;
InnerTable. AddCell (innerCell2 );
Cell. AddElement (innerTable );
At this time, if you view the generated PDF file, you will find many problems. First, the cell's border line and the border line of the embedded table will be displayed. Second, the embedded table does not fill the entire cell, white around, especially on both sides. For problem 1, you can set the Border width of the Peripheral Cells to 0 and only display the border lines of the embedded table. Problem 2: You need to set the Padding attribute of the peripheral cell. You can also set the attribute for one side separately, which is similar to CSS. The procedure is as follows:
Cell. BorderWidth = 0;
Cell. Padding = 0;
Check the generated document and find that the upper and lower sides are correct, but the left and right sides still have a wide white space. It seems that iTextSharp has a default width percentage for Embedded tables, leading to blank spaces on both sides. We need to set the table width as follows:
InnerTable. WidthPercentage = 100; // set the table width percentage, which is relative to the width of the parent element.
Check the document again. This is correct. But in the end, there is another problem. It is impossible to divide every cell equally. How can we set the cell width? As follows:
InnerTable. SetWidths (new int [] {10, 90}); // you can specify the column width in percent.
Here, the basic table is complete.
From orain's column