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?

7 Answers 7

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

1
  • 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
    Commented May 23, 2014 at 12:35
19

In my app it works like this:

PdfPTable table = new PdfPTable(2);
table.getDefaultCell().setBorder(0);
...
2
  • 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
    Commented Dec 2, 2014 at 7: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
    Commented Sep 11, 2015 at 1:34
6

The below works for me.

table.getDefaultCell().setBorderWidth(0f);
2
  • If ItextSharp 5+ ; table.DefaultCell.BorderWidth = 0f works for the default cells only.
    – Anass
    Commented Nov 8, 2018 at 15:54
  • thnx , its working good Commented Jan 31, 2023 at 6:46
2

For iText 7

Table table = new Table();
table.SetBorder(Border.NO_BORDER);
3
  • 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
  • You can define at table and cell Commented Apr 21, 2022 at 1:27
  • Technically you can for some reason, but it has no effect. At least in Itext7. Commented Apr 21, 2022 at 6: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);
2
  • 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
    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
1

set cell color white.

cellOne.setBorderColor(BaseColor.WHITE);
1

you can hide the border like this

PdfPCell cell = new PdfPCell ();
cell.setBorder(Rectangle.NO_BORDER);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.