How to use a dotted line as a cell border?

Tags: tablestable borderbordercell eventdotted lineiText 7

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. For the first table, we use a table renderer:

private class DottedLineTableRenderer extends TableRenderer {
    public DottedLineTableRenderer(Table modelElement, Table.RowRange rowRange) {
        super(modelElement, rowRange);
    public void drawChildren(DrawContext drawContext) {
        PdfCanvas canvas = drawContext.getCanvas();
        canvas.setLineDash(3f, 3f);
        // first horizontal line
        CellRenderer[] cellRenderers = rows.get(0);
        canvas.lineTo(cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getRight(),
                cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getTop());
        for (int i = 0; i < rows.size(); i++) {
            cellRenderers = rows.get(i);
            // horizontal lines
            canvas.lineTo(cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getRight(),
                    cellRenderers[cellRenderers.length - 1].getOccupiedArea().getBBox().getBottom());
            // first vertical line
            Rectangle cellRect = cellRenderers[0].getOccupiedArea().getBBox();
            canvas.moveTo(cellRect.getLeft(), cellRect.getBottom());
            canvas.lineTo(cellRect.getLeft(), cellRect.getTop());
            // vertical lines
            for (int j = 0; j < cellRenderers.length; j++) {
                cellRect = cellRenderers[j].getOccupiedArea().getBBox();
                canvas.moveTo(cellRect.getRight(), cellRect.getBottom());
                canvas.lineTo(cellRect.getRight(), cellRect.getTop());

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 renderer:

private class DottedLineCellRenderer extends CellRenderer {
    public DottedLineCellRenderer(Cell modelElement) {
    public void draw(DrawContext drawContext) {
        drawContext.getCanvas().setLineDash(3f, 3f);

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.

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