简体   繁体   English

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

[英]How to set a background color of a Table Cell using iText?

While it is of course possible to use BaseColor , by default, it offers very limited choices. 虽然当然可以使用BaseColor ,但默认情况下它提供的选择非常有限。

I wonder how can i add my own custom color to the document? 我想知道如何将自己的自定义颜色添加到文档中?

...
        PdfPTable table = new PdfPTable(3);

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

Posting, in hopes someone else will find this response useful. 发布,希望其他人会发现此回复有用。

It seems one can create a new BaseColor from WebColor as: 似乎可以从WebColor创建一个新的BaseColor

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

Which then can be added as a background as: 然后可以将其添加为背景:

cell.setBackgroundColor(myColor);

Lots of options. 很多选择。

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

There's also pattern colors and shading colors, but those are Much Less Simple. 还有图案颜色和阴影颜色,但这些颜色要简单得多。

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

One more solution is: 另一个解决方案是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM