iText pdf library
Website search

How do I change the border color of a PdfPCell?

I am trying to set border colors of table cells. No matter what I try, the border color is not changing - it's always black! What am I doing wrong?

Here's my test code. cell1 should have a red top border and a blue bottom border:


PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));
cell1.setBorderColorTop(new BaseColor(255, 0, 0));
PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));
Question posted on StackOverflow on Jan 29, 2016 by Naresh

Please take a look at the ColoredBorder example. It shows the solutions of your problem.

Here is a short piece of code that shows how to create a cell with different borders in iText 7:

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
Table table;
table = new Table(2);
Cell cell;
cell = new Cell().add("Cell 1");
cell.setBorderTop(new SolidBorder(Color.RED, 1));
cell.setBorderBottom(new SolidBorder(Color.BLUE, 1));
cell = new Cell().add("Cell 2");

The result looks like this:

Different cell border widths and colors

Different cell border widths and colors

Note that SolidBorder constructor has two parameters: the color and the width. You can chose any type of border you need: DashedBorder, DottedBorder, DoubleBorder, GrooveBorder, InsetBorder, OutsetBorder, RidgeBorder, RoundDotsBorder.

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


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