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?
7 Answers
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
-
1I tried both independently and both seem to make the border invisible. What exactly is the difference between
cellOne.setBorder(Rectangle.NO_BORDER);
andcellTwo.setBorder(Rectangle.BOX);
?– QoheletCommented May 23, 2014 at 12:35
In my app it works like this:
PdfPTable table = new PdfPTable(2);
table.getDefaultCell().setBorder(0);
...
-
3
Rectangle.NO_BORDER
is the same as0
I'd prefer to use the constants name, easier to read and always compatible when upgrading in version. Commented Dec 2, 2014 at 7:46 -
3This method does not appear to work in version 5.5.6. Sean's answer is correct: cellOne.setBorder(Rectangle.NO_BORDER);– StevenCommented Sep 11, 2015 at 1:34
The below works for me.
table.getDefaultCell().setBorderWidth(0f);
-
If ItextSharp 5+ ; table.DefaultCell.BorderWidth = 0f works for the default cells only.– AnassCommented Nov 8, 2018 at 15:54
-
For iText 7
Table table = new Table();
table.SetBorder(Border.NO_BORDER);
-
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". kb.itextpdf.com/home/it7kb/faq/… Commented Apr 18, 2022 at 18:25
-
-
Technically you can for some reason, but it has no effect. At least in Itext7. Commented Apr 21, 2022 at 6:42
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);
-
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. Commented Jul 30, 2014 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. Commented Dec 10, 2015 at 16:28
you can hide the border like this
PdfPCell cell = new PdfPCell ();
cell.setBorder(Rectangle.NO_BORDER);