1

I have seen how to set rounded borders for a table cell in this question

How to create a rounded corner table using iText\iTextSharp?

But is it possible to make cell that will have no borders, but colored and rounded background?

Community
  • 1
  • 1
filipst
  • 1,547
  • 1
  • 30
  • 55

1 Answers1

2

To achieve that, you need cell events. I've provided different examples in my book. See for instance calendar.pdf:

enter image description here

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();
    }
}

For the C# version of this code, go to Where do I find the C# examples? and click on the chapter that corresponds with the chapter of the Java version of the example.

For instance, PdfCalendar.cs:

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 this event like this:

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

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

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165