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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9417054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:19:12  来源:igfitidea点击:

ITextSharp: Set table cell border color

c#itext

提问by Luke101

How do I set the border color of a table cell. here is the code i have:

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

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

As you can see i am setting the color two different ways and neither way is working. When the table is rendered the border is always black. How can I fix this.

如您所见,我正在以两种不同的方式设置颜色,但两种方式都不起作用。渲染表格时,边框始终为黑色。我怎样才能解决这个问题。

采纳答案by kuujinbo

When you set individualcell border properties you either need to set allborder colors and widths individually, or explicitly set the UseVariableBordersproperty to true. Try this example to see what I mean:

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