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.

24th October 2015
admin-marketing

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.


Share this article

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now