如何使用iText设置表格单元格的背景颜色?

虽然当然可以使用BaseColor ,但默认情况下它提供的选择非常有限。

我想知道如何将自己的自定义颜色添加到文档中?

 ... PdfPTable table = new PdfPTable(3); PdfPCell cell = new PdfPCell(new Phrase("some clever text")); cell.setBackgroundColor(BaseColor.GREEN); table.addCell(cell); ... 

很多选择。

 BaseColor color = new BaseColor(red, green, blue); // or red, green, blue, alpha CYMKColor cmyk = new CMYKColor(cyan, yellow, magenta, black); // no alpha GrayColor gray = new GrayColor(someFloatBetweenZeroAndOneInclusive); // no alpha 

还有图案颜色和阴影颜色,但这些颜色要简单得多。

发布,希望其他人会发现此回复有用。

似乎可以从WebColor创建一个新的BaseColor

 BaseColor myColor = WebColors.GetRGBColor("#A00000"); 

然后可以将其添加为背景:

 cell.setBackgroundColor(myColor); 

尝试这个:
cell.setBackgroundColor(new BaseColor(226, 226, 226));
要么:
cell.setBackgroundColor(WebColors.getRGBColor("#E2E2E2")); 弃用

另一个解决方案是:

 public static String mColor = "#aa8cc5"; int aa = Integer.parseInt(mColor,16); // base 16 int colorArr = Color.rgb(Color.red(aa),Color.green(aa),Color.blue(aa)); cell1.setBackgroundColor(new BaseColor(colorArr));