javapdfitextpdf-generation

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.

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

Solution

  • 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.