Legacy notice!

iText 5 is the previous major version of iText's leading PDF SDK. iText 5 has been EOL, and is no longer developed. Switch your project to iText 7, integrating the latest developments.
Check related iText 7 content!

I have seen how to create cells with rounded borders, but is it possible to make a cell that will have no borders, but colored and rounded background.

Posted on StackOverflow on Nov 7, 2014 by filipst

To achieve that, you need cell events. I've provided different examples in iText in Action (second edition):

Colored cells

Colored cells

The Java code to create the white cells looks like this:

class CellBackground implements PdfPCellEvent {
    public void cellLayout(PdfPCell cell, Rectangle rect,
            PdfContentByte[] canvas) {
        PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
        cb.roundRectangle(
            rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3,
            rect.getHeight() - 3, 4);
        cb.setCMYKColorFill(0x00, 0x00, 0x00, 0x00);
        cb.fill();
    }
}

In C#, the code looks like this:

class CellBackground : IPdfPCellEvent {
  public void CellLayout(
    PdfPCell cell, Rectangle rect, PdfContentByte[] canvas
) {
    PdfContentByte cb = canvas[PdfPTable.BACKGROUNDCANVAS];
    cb.RoundRectangle(
      rect.Left + 1.5f,
      rect.Bottom + 1.5f,
      rect.Width - 3,
      rect.Height - 3, 4
    );
    cb.SetCMYKColorFill(0x00, 0x00, 0x00, 0x00);
    cb.Fill();
  }
}

You can use:

CellBackground cellBackground = new CellBackground();
cell.CellEvent = cellBackground;

Now the CellLayout() method will be executed the moment the cell is rendered to a page.

Click this link if you want to see how to answer this question in iText 7.