ITextSharp: Set table cell border color

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

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.

回答1:

When you set individual cell border properties you either need to set all border colors and widths individually, or explicitly set the UseVariableBorders property to true. Try this example to see what I mean:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!