36

I am using iText library for generating pdf files in Java. I am writing data in pdfptable , how can I make the borders of table invisible?

yogsma
  • 10,142
  • 31
  • 97
  • 154

7 Answers7

74

The Border Elements of the PdfPTable are defined by the PdfPCell which are added to the table. Each Cell will have its own style/formatting. Here is the API: http://api.itextpdf.com/

Example

PdfPTable table = new PdfPTable(2);
PdfPCell cellOne = new PdfPCell(new Phrase("Hello"));
PdfPCell cellTwo = new PdfPCell(new Phrase("World"));

cellOne.setBorder(Rectangle.NO_BORDER);
cellOne.setBackgroundColor(new Color(255,255,45));

cellTwo.setBorder(Rectangle.BOX);

table.addCell(cellOne);
table.addCell(cellTwo);

If you want more detail about the Rectangle/Border values, take a look at the IText Constant values section for Rectangle, here : http://api.itextpdf.com/constant-values.html

shareef
  • 9,255
  • 13
  • 58
  • 89
Sean
  • 7,597
  • 1
  • 24
  • 26
  • 1
    I tried both independently and both seem to make the border invisible. What exactly is the difference between `cellOne.setBorder(Rectangle.NO_BORDER);` and `cellTwo.setBorder(Rectangle.BOX);`? – Qohelet May 23 '14 at 12:35
18

In my app it works like this:

PdfPTable table = new PdfPTable(2);
table.getDefaultCell().setBorder(0);
...
Sura Chaitanya
  • 243
  • 1
  • 4
  • 11
  • 3
    `Rectangle.NO_BORDER` is the same as `0` I'd prefer to use the constants name, easier to read and always compatible when upgrading in version. – chillworld Dec 02 '14 at 07:46
  • 3
    This method does not appear to work in version 5.5.6. Sean's answer is correct: cellOne.setBorder(Rectangle.NO_BORDER); – Steven Sep 11 '15 at 01:34
6

The below works for me.

table.getDefaultCell().setBorderWidth(0f);
Sport
  • 8,570
  • 6
  • 46
  • 65
sharon
  • 97
  • 1
  • 2
2

For iText 7

Table table = new Table();
table.SetBorder(Border.NO_BORDER);
Kevin Montalvo
  • 751
  • 7
  • 5
  • This will _not_ work. "Borders are defined at the level of the cell, not at the level of the table. Hence if you want to remove the borders of the table, you need to remove the borders of each cell". https://kb.itextpdf.com/home/it7kb/faq/how-to-draw-a-borderless-table-in-itextsharp – Artur INTECH Apr 18 '22 at 18:25
  • You can define at table and cell – Kevin Montalvo Apr 21 '22 at 01:27
  • Technically you can for some reason, but it has no effect. At least in Itext7. – Artur INTECH Apr 21 '22 at 06:42
1
    PdfPTable nestedTable = new PdfPTable();
    nestedTable.DefaultCell.Border = 0;

    nestedTable.AddCell(new Phrase("First");
    nestedTable.AddCell(new Phrase("Second");
    nestedTable.AddCell(new Phrase("2515");

    PdfPCell nestCell= new PdfPCell(nestedTable);
user953005
  • 27
  • 1
  • 1
    `nestedTable.DefaultCell.Border = 0;` - that just doesn't look right. Upon testing i also found that doesn't work. Unless the DefaultCell and Border properties are public this could never work. – wmdvanzyl Jul 30 '14 at 11:49
  • It could work - but only in C# (seeing the case of Properties and Method calls), while OP asked about Java solution. Also, there are parenthesis missing in lines 4-6. – mareckmareck Dec 10 '15 at 16:28
1

set cell color white.

cellOne.setBorderColor(BaseColor.WHITE);
Lucifer
  • 29,392
  • 25
  • 90
  • 143
NITIN RATHOUR
  • 161
  • 2
  • 3
1

you can hide the border like this

PdfPCell cell = new PdfPCell ();
cell.setBorder(Rectangle.NO_BORDER);
Madhuka Dilhan
  • 1,396
  • 1
  • 14
  • 21