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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6405623/
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-16 05:39:37  来源:igfitidea点击:

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

javapdfitextpdf-generation

提问by James Raitsev

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

采纳答案by Mark Storer

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.

还有图案颜色和底纹颜色,但这些都不那么简单。

回答by James Raitsev

Posting, in hopes someone else will find this response useful.

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

It seems one can create a new BaseColorfrom WebColor as:

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

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

Which then can be added as a background as:

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

cell.setBackgroundColor(myColor);

回答by Sagar Shah

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

回答by Tony Chen

Try this:
cell.setBackgroundColor(new BaseColor(226, 226, 226));
or:
cell.setBackgroundColor(WebColors.getRGBColor("#E2E2E2"));deprecated

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