2012-02-23 171 views
16

如何設置表格單元格的邊框顏色。這裏是我有的代碼:ITextSharp:設置表格單元格邊框顏色

// create and define table 
var table = new PdfPTable(8); 
table.HorizontalAlignment = Element.ALIGN_CENTER; 

//table.HeaderRows = 1; 

// the cell object 
PdfPCell cell; 
var f = FontFactory.GetFont("Tahoma", 11, Font.BOLD); 

cell = new PdfPCell(new Phrase("Source Review", f)); 
cell.BorderColorLeft = new BaseColor(255, 255, 255); 
cell.BorderColorRight = new iTextSharp.text.BaseColor(255, 255, 255); 
table.AddCell(cell); 

正如你可以看到我設置顏色兩種不同的方式,並沒有辦法工作。表格呈現時,邊框始終爲黑色。我怎樣才能解決這個問題。

回答

27

當您設置個人單元格邊框屬性,你要麼需要單獨設置所有邊框顏色和寬度,或明確的UseVariableBorders屬性設置爲true。試試這個例子來看看我的意思:

PdfPTable table = new PdfPTable(1); 
PdfPCell cell = new PdfPCell(new Phrase("test 1")); 
cell.UseVariableBorders = true; 
cell.BorderColorLeft = BaseColor.BLUE; 
cell.BorderColorRight = BaseColor.ORANGE; 
table.AddCell(cell); 

cell = new PdfPCell(new Phrase("test 2")); 
cell.BorderColorLeft = BaseColor.RED; 
cell.BorderColorRight = BaseColor.GREEN; 
cell.BorderColorTop = BaseColor.PINK; 
cell.BorderColorBottom = BaseColor.YELLOW; 
cell.BorderWidthLeft = 1f; 
cell.BorderWidthRight = 1f; 
cell.BorderWidthTop = 1f; 
cell.BorderWidthBottom = 1f; 
table.AddCell(cell); 

cell = new PdfPCell(new Phrase("test 3")); 
cell.BorderColor = BaseColor.GREEN; 
table.AddCell(cell);