A table is an element that we often use when we create a document. The control of the layout is very precise. The table object in ITextSharp is the following two elements:
PdfTable,PdfCell
Let's take a piece of code from ITextSharp In Action:
From the code, you can see that the constructor of PdfTable, passing in a column number as a parameter, indicates how many columns in this table, adding PdfCell to the table, if the number of cells added is more than one row, it will automatically wrap. There is a setColspan function in the cell (note: in the C# version, the property Colspan), used to set a cell across multiple columns. Similarly, if you want to span multiple lines, there is also a property (C#) RolSpan. The above demo code execution results are as follows:
The PdfTable object has a function that sets the table area and the width of each column, as follows:
public void SetTotalWidth(float[] columnWidth);
public void SetWidthPercentage(float[] columnWidth, Rectangle pageSize);
Property: HorizontalAlignment Sets the alignment of the table
HeaderRows represents the first few lines as the table header
FooterRows represents the first few lines as the end of the table
SplitLate indicates whether the cell is displayed across pages
SplitRows indicates whether the line is displayed across pages
The sample code is as follows:
1: public class PdfPTableDemo : TestBase
2: {
3: protected override void Opening(Document document, PdfWriter writer)
4: {
5: document.SetPageSize(PageSize.A4.Rotate());
6: base.Opening(document, writer);
7: }
8: protected override void WriteDocument(Document document, PdfWriter writer)
9: {
10: PdfPTable table = CreateTable();
11: //table.WidthPercentage = 80;//Set the width of the table, percentage
12: //table.TotalWidth = 200; / / set the width of the table, the unit points
13: //table.SetTotalWidth();
14: //table.SetWidthPercentage();
15: table.HorizontalAlignment = Element.ALIGN_LEFT;
16: document.Add(table);
17:
18: table = CreateTable();
19: table.HorizontalAlignment = Element.ALIGN_RIGHT;
20: table.SpacingBefore = (5);
21: table.SpacingAfter = (5);
22: Rectangle rect = new Rectangle(523, 770);
23: table.SetWidthPercentage(
24: new float[] { 50, 25, 25 }, rect);
25: document.Add(table);
26: table = CreateTable();
27: table.HorizontalAlignment = Element.ALIGN_CENTER;
28: table.SetTotalWidth(new float[] { 144, 72, 72 });
29: table.LockedWidth = (true);
30: document.Add(table);
31: table = CreateTable();
32: table.SpacingBefore = (15);
33: table.SpacingAfter = (15);
34: document.Add(table);
35:
36: table = new PdfPTable(3);
37: PdfPCell cell
38: = new PdfPCell(new Phrase("Head test", Normal));
39: cell.BackgroundColor = (BaseColor.YELLOW);
40: cell.HorizontalAlignment = (Element.ALIGN_CENTER);
41: cell.Colspan = (7);
42: table.AddCell(cell);
43:
44: cell
45: = new PdfPCell(new Phrase("Tail test", Normal));
46: cell.BackgroundColor = (BaseColor.YELLOW);
47: cell.HorizontalAlignment = (Element.ALIGN_CENTER);
48: cell.Colspan = (7);
49: table.AddCell(cell);
50:
51: table.DefaultCell.BackgroundColor = (BaseColor.LIGHT_GRAY);
52: for (int i = 0; i < 100; i++)
53: {
54: table.AddCell("Location");
55: table.AddCell("Time");
56: table.AddCell("Run Length");
57: table.AddCell("Title");
58: table.AddCell("Year");
59: table.AddCell("Directors");
60: table.AddCell("Countries");
61: }
62: table.DefaultCell.BackgroundColor = (null);
63: table.HeaderRows = (2);
64: table.FooterRows = (1);
65: document.Add(table);
66:
67: }
68:
69: private PdfPTable CreateTable()
70: {
71: PdfPTable table = new PdfPTable(3);
72: table.TableEvent = new AlternatingBackground();
73: //table.WidthPercentage = 80;//Set the width of the table, percentage
74: //table.TotalWidth = 200; / / set the width of the table, the unit points
75: //table.SetTotalWidth();
76: //table.SetWidthPercentage();
77: PdfPCell cell;
78: cell = new PdfPCell(new Phrase("Cell with colspan 3"));
79: cell.Colspan = (3);
80: table.AddCell(cell);
81: cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
82: cell.Rowspan = (2);
83: table.AddCell(cell);
84: table.AddCell("row 1; cell 1");
85: table.AddCell("row 1; cell 2");
86: table.AddCell("row 2; cell 1");
87: table.AddCell("row 2; cell 2");
88:
89: return table;
90: }
91: }
92:
2011-07-14
Add one more thing: When setting the alignment of cells, you should choose to set the alignment and then add content.
1: cell = new PdfPCell(new Chunk("Chinese people",Normal));
2: cell.UseAscender = (true);
3: cell.UseDescender = (true);
4: cell.VerticalAlignment = Element.ALIGN_MIDDLE;
5: cell.HorizontalAlignment = Element.ALIGN_CENTER;
7: table.AddCell(cell);
Step by step IText.Sharp