原文:http://www.cnblogs.com/LifelongLearning/archive/2011/06/22/2086802.html
PdfPTable和PdfPCell對象,我們可以制作出豐富多彩的表格,可以制作出跨行、跨列,不同表格線,單元格中的文字旋轉等效果,如下所示:
1、文本模式:
PdfPCell cell = new PdfPCell(new Paragraph("some text"));
2、組合模式:
PdfPCell cell = new PdfPCell(); cell.AddElement(new Paragraph("some text"));
這兩種區別:
文本模式下,對於內容的對齊方式、間距、縮進使用的是單元格來屬性進行控制,在組合模式下,使用對象自身的屬性進行控制。
在表格中,對單元格的邊框進行隱藏和設置顏色,
PdfPCell中有一個屬性:Border 邊框屬性
此值使用如果下值進行設置:
1: public class Rectangle : Element, IElement
2: {
3: public const int BOTTOM_BORDER = 2;
4: public const int BOX = 15;
5: public const int LEFT_BORDER = 4;
6: public const int NO_BORDER = 0;
7: public const int RIGHT_BORDER = 8;
8: public const int TOP_BORDER = 1;
並且可以組合使用,如:Rectangle.TOP_BORDER|Rectangle.LEFT_BORDER
不要通過設置BorderWidth=0來進行隱藏邊框,這樣會造成表格對不齊。
如下圖所示:
第一行默認對齊左邊框設置為寬度為0,明顯看出右邊沒有對齊。
正常情況:
通過設置Boder隱藏邊框:
代碼如下:
1: cell = CreateCell("默認對齊", useAscender, useDescender, null, null);
2: cell.Border = Rectangle.RIGHT_BORDER | Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER;
3: //cell.UseBorderPadding = false;
4: // cell.BorderWidthLeft = 0;
5: //cell.BorderColorTop = BaseColor.BLUE;
6: //cell.BorderWidthTop = 1;
7:
8: table.AddCell(cell);
顯示效果:
對於邊框顏色設置,如果要對邊框整個設置顏色,可以使用BoderColor進行,如果只對單獨表格線設置顏色,就發使用BorderColorTop,BorderColorLeft等屬性進行,並且要設置其寬度,否則顏色顯示不出來。
代碼:
1: cell = CreateCell("默認對齊", useAscender, useDescender, null, null);
2: cell.Border = Rectangle.RIGHT_BORDER | Rectangle.TOP_BORDER | Rectangle.BOTTOM_BORDER;
3: cell.BorderColor = BaseColor.RED;
4: cell.BorderColorTop = BaseColor.BLUE;
5: cell.BorderWidthTop = 0.5f;
6:
7: table.AddCell(cell);
顯示效果: