ITextSharp:设置表格单元格边框颜色

20

如何设置表格单元格的边框颜色。这是我目前的代码:

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

你可以看到,我使用了两种不同的方式设置颜色,但都没有生效。当表格被渲染时,边框总是黑色的。我应该如何修复这个问题。

1个回答

32

当您设置单个单元格边框属性时,您需要逐个设置所有边框颜色和宽度,或者显式设置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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接