2012-02-23 39 views
16

Làm cách nào để đặt màu đường viền của ô bảng. đây là mã tôi có:ITextSharp: Đặt màu viền ô của bảng

// 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); 

Như bạn có thể thấy tôi đang thiết lập màu theo hai cách khác nhau và cả hai cách đều không hoạt động. Khi bảng được hiển thị, đường viền luôn màu đen. Làm thế nào tôi có thể sửa lỗi này.

Trả lời

27

Khi bạn thiết lập tính biên giới tế bào riêng lẻ bạn có cần phải thiết lập tất cả màu đường viền và độ rộng riêng biệt, hoặc thiết lập một cách rõ ràng UseVariableBorders tài sản để true. Hãy thử ví dụ này để xem ý tôi là:

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); 
Các vấn đề liên quan