2

I have to create a table having rounded corners, something like it:

enter image description here

Can I do it with iTextSharp?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

9

This is done using cell events.

See the Calendar example from my book (Java / C#).

Make sure that you don't add any "automated" borders to the cell, but draw the borders yourself in a cell event:

table.DefaultCell.Border  = PdfPCell.NO_BORDER;
table.DefaultCell.CellEvent = new RoundedBorder();

The RoundedBorder class would then look like this:

class RoundedBorder : 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.Stroke();
  }
}

You can of course fine-tune the values 1.5, 3 and 4 to get different effects.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    I understand your example but when I tryied to apply it into my example (I need it into a footer table) don't work and I still have classical corner, I have create a new question related to the specific case at this link: http://stackoverflow.com/questions/23652856/why-i-cant-obtain-rounded-table-corners-in-this-itext-itextsharp-footer-table If for you is not a problem can you help me? :-) TNX – AndreaNobili May 14 '14 at 11:06
  • 1
    I've answered that question. – Bruno Lowagie May 14 '14 at 12:15
  • @BrunoLowagie If I would like to have a backgroundcolor for the cell, but contained only within the rounded border. How would you solve that? – Daniel Wallman Nov 27 '20 at 10:57
  • I managed to solve it by setting cb.setColorFill() and then using cb.fill() instead of cb.stroke() – Daniel Wallman Nov 27 '20 at 10:59