Skip to main content
Skip table of contents

How to create a table with only vertical borders?

It is possible with iText 7 to create a table with vertical lines only?

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. How to do this, depends on how you add the cells to the table.

These are the two approaches:

1. You create PdfPCell objects explicitly:

PdfPCell cell = new PdfPCell();
cell.AddElement(new Paragraph("my content"));
cell.Border = PdfPCell.LEFT;
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.Border = PdfPCell.LEFT | PdfPCell.RIGHT;

2. You create PdfPCell objects implicitly:

In this case, you don't create a PdfPCell object yourself, you let iTextSharp create the cells. These cells will copy the properties of the DefaultCell that is defined at the level of the PdfPTable, so you need to change this:

table.DefaultCell.Border = Rectangle.LEFT | Rectangle.RIGHT;
table.addCell("cell 1");
table.addCell("cell 2");
table.addCell("cell 3");

Now all the cells won't have a top or bottom border, only a left and right border. You'll be drawing some extra lines, but nobody will notice as the lines coincide.

For instance:

PdfPTable table = new PdfPTable(5);
table.TotalWidth = 510f;//table size
table.LockedWidth = true;
table.SpacingBefore = 10f;//both are used to mention the space from heading
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.DefaultCell.Border = PdfPCell.LEFT | PdfPCell.RIGHT;
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);
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.