Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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); ... 
like image 361
James Raitsev Avatar asked Jun 19 '11 23:06

James Raitsev


2 Answers

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); 
like image 132
James Raitsev Avatar answered Sep 20 '22 22:09

James Raitsev


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.

like image 23
Mark Storer Avatar answered Sep 21 '22 22:09

Mark Storer