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

76,976

Solution 1

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.

Solution 2

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

It seems one can create a new BaseColor from WebColor as:

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

Which then can be added as a background as:

cell.setBackgroundColor(myColor);

Solution 3

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

Share:
76,976
James Raitsev
Author by

James Raitsev

I ask a lot of questions. Some of them are good.

Updated on August 16, 2020

Comments

  • James Raitsev
    James Raitsev almost 4 years

    While it is of course possible to use BaseColor, by default, it offers very limited choices.

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