Skip to main content
Skip table of contents

How to introduce rounded cells with a background color?

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];
            rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3,
            rect.getHeight() - 3, 4);
        cb.setCMYKColorFill(0x00, 0x00, 0x00, 0x00);

In C#, the code looks like this:

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

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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.