3

I need to create a table which has custom colored cells and borders. There are a few constants defined in the Color class, but what I need a custom color. I need #a6cb0b as the background color for the header and border lines with color code #cccccc. How do I set them?

Table table = new Table(new float[]{1,1,1});
Cell cell = new Cell();
cell.add(new Paragraph("TITLE"));
cell.setBackgroundColor(Color.???);
table.addCell(cell);
...
...
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
nitinkaveriappa
  • 91
  • 2
  • 10

1 Answers1

9

The best way to find out how to create colors, is to check the API docs. When you go to the page that describes the 'Color' class, you see that it has several subclasses:

It seems that you want to create an RGB color, hence you need DeviceRgb:

Color headerBg = new DeviceRgb(0xA6, 0xCB, 0x0B);
Color lineColor = new DeviceRgb(0xCC, 0xCC, 0xCC);

You can use the color object to set the color of borders, backgrounds, etc...

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I did read the API docs for 'Color' class but I could not figure out the way I need to pass the color values to DeviceRgb. Anyways Thank you @BrunoLowagie. – nitinkaveriappa Oct 31 '17 at 15:51
  • I am new to iText so this might be a very simple question. The `cell` class has a `setBackground()` function that can be used to set the color of the background. But how do I set the Border of the Cell to `lineColor`? I tried `cell.setBorder(new Border().setColor(lineColor));`. @BrunoLowagie – nitinkaveriappa Oct 31 '17 at 16:11
  • You can't create a `Border` instance, can you? You need to create a *specific* border, such as a `SolidBorder` or a `DottedBorder`. You may also want to specify a width, for instance `new SolidBorder(lineColor, 3)`. – Bruno Lowagie Oct 31 '17 at 16:25
  • Thanks once again. @BrunoLowagie – nitinkaveriappa Oct 31 '17 at 16:32