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

James Raitsev picture James Raitsev · Jun 20, 2011 · Viewed 69.7k times · Source

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

Answer

James Raitsev picture James Raitsev · Jun 20, 2011

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