How to use a dotted line as a cell border?

I am trying to create a table with cells that have a dotted line for a border. How can I do this?

Posted on StackOverflow on Nov 21, 2013 with user1913695

I've made an example that solves your problem: DottedLineCell; The resulting PDF is a document with two tables: dotted_line_cell.pdf

For the first table, we use a table event:

class DottedCells implements PdfPTableEvent {
    public void tableLayout(PdfPTable table, float[][] widths,
        float[] heights, int headerRows, int rowStart,
        PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setLineDash(3f, 3f);
        float llx = widths[0][0];
        float urx = widths[0][widths[0].length -1];
        for (int i = 0; i 

This is the most elegant way to draw the cell borders, as it uses only one stroke() operator for all the lines. Unfortunately, this solution isn't an option if you have tables with rowspans.

The second table uses a cell event:

class DottedCell implements PdfPCellEvent {
    public void cellLayout(PdfPCell cell, Rectangle position,
        PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setLineDash(3f, 3f);
        canvas.rectangle(position.getLeft(), position.getBottom(),
            position.getWidth(), position.getHeight());

With a cell event, a border is drawn around every cell. This means you'll have multiple stroke() operators and overlapping lines. However: this solution always works, also when the table has cells with a rowspan greater than one.

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

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