How to create a table with only vertical borders?

I have a table with vertical and horizontal lines. But I do not want horizontal line. I want only vertical lines as shown in this example: Currently, my table code looks like this: PdfPTable

I have a table with vertical and horizontal lines. But I do not want horizontal line. I want only vertical lines as shown in this example:

table with only vertical borders

Currently, my table code looks like this:

PdfPTable table = new PdfPTable(5);
table.AddCell(new Phrase("    SL.NO", font1));
table.AddCell(new Phrase("   SUBJECTS", font1));
table.AddCell(new Phrase("   MARKS", font1));
table.AddCell(new Phrase("   MAX MARK", font1));
table.AddCell(new Phrase("   CLASS AVG", font1));
Doc.Add(table);
Posted on StackOverflow on Sep 25, 2015 by Semil Sebastian

You can change the borders of the cells so that they only show the vertical lines. If you are using iText 7 note that now we use Table instead of PdfPTable and Cell instead of PdfPCell.

You should create a Cell instance and set the border to NO_BORDER value, then chose the border type for the left side and use .setBorderLeft() method:

Cell cell = new Cell().add("SL.NO")
            .setBorder(Border.NO_BORDER)
            .setBorderLeft(new SolidBorder(1));
table.addCell(cell);

In this case, only the left border of the cell will be shown. For the last cell in the row you should also add the right border:

cell.setBorderRight(new SolidBorder(1));

Click this link if you want to see how to answer this question in iText 5.


Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now