iText pdf library
Website search

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.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

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