9

This code below does not work.

Table table = new Table(2); 
table.setBorder(Border.NO_BORDER);

I am new to iText 7 and all I wanted is to have my table borderless. Like how to do it?

rhens
  • 4,791
  • 3
  • 22
  • 38
The Newbie
  • 151
  • 1
  • 1
  • 12

2 Answers2

16

The table itself is by default not responsible for borders in iText7, the cells are. You need to set every cell to be borderless if you want a borderless table (or set the outer cells to have no border on the edge if you still want inside borders).

Cell cell = new Cell();
cell.add("contents go here");
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
Samuel Huylebroeck
  • 1,639
  • 11
  • 15
  • I tried this one sir but an error occurred in "cell.setBorder(Border.NO_BORDER)" saying CANNOT FIND SYMBOL "NO_BORDER" – The Newbie Jan 13 '17 at 01:45
  • ah sorry my problem. import problem. tnxxx – The Newbie Jan 13 '17 at 02:12
  • 3
    As per [iText 7.1.6](http://itextsupport.com/apidocs/itext7/7.1.6/com/itextpdf/layout/element/Cell.html) you can't add a `String` to `Cell` anymore, only `IBlockElement` or `Image`. – Evgenij Reznik May 02 '19 at 10:53
  • 1
    iText 7.1.10 with C# you can do it that way: .table.AddCell(new Cell().Add(new Paragraph("contents").SetBorder(Border.NO_BORDER))); – alex Mar 26 '20 at 22:38
11

You could write a method which runs though all children of a Table and sets NO_BORDER.

private static void RemoveBorder(Table table)
{
    for (IElement iElement : table.getChildren()) {
        ((Cell)iElement).setBorder(Border.NO_BORDER);
    }
}

This gives you the advantage that you can still use

table.add("whatever");
table.add("whatever");
RemoveBorder(table);

instead of changing it on all cells manual.

Netzmensch
  • 111
  • 1
  • 7