原文:http://www.cnblogs.com/LifelongLearning/archive/2011/05/16/2048116.htmlhtml
表格是咱們在製做文檔時,常用的一個元素。對佈局的控制很是精確。在ITextSharp中表格對象是下面兩個元素:app
PdfTable,PdfCellide
下面從ITextSharp In Action截取一段代碼:函數
從代碼中,能夠看出,PdfTable的構造函數,傳入一個列數爲參數,表示這個表格有多少列,往表格中加入PdfCell,若是加入的單元格超過一行,自動會進行換行。單元格中有一個setColspan函數(注:C#版本中,是屬性Colspan),用於設置一個單元格跨多列。一樣,若是要跨越多行,也有一個屬性(C#)RolSpan,上面的演示代碼執行結果以下:佈局
PdfTable對象有一個設置表格區域和每列寬度的函數,以下:測試
public void SetTotalWidth(float[] columnWidth); public void SetWidthPercentage(float[] columnWidth, Rectangle pageSize);spa
屬性:HorizontalAlignment 設置表格的對齊方式3d
HeaderRows 表示第幾行做爲表格頭code
FooterRows 表示第幾行做爲表格尾orm
SplitLate 表示單元格是否跨頁顯示
SplitRows 表示行是否跨頁顯示
示例代碼以下:
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;//設置表格佔的寬度,百分比
12: //table.TotalWidth = 200;//設置表格佔的寬度,單位點數
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("表頭測試", 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("表尾測試", 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;//設置表格佔的寬度,百分比
74: //table.TotalWidth = 200;//設置表格佔的寬度,單位點數
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:
補充一點:在設置單元格的對齊方式時,應該選設置對齊方式,再來添加內容。
1: cell = new PdfPCell(new Chunk("中國人民",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);