Table and cell events to draw borders

Tags: tablestable borderbordercell eventdotted linerounded cornersiText 7
Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/35746651
 */
 
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class CellTitle extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/cell_title.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CellTitle().manipulatePdf(DEST);
    }
 
    public Cell getCell(String content, String title) {
        Cell cell = new Cell().add(content);
        cell.setNextRenderer(new CellTitleRenderer(cell, title));
        cell.setPaddingTop(8).setPaddingBottom(8);
        return cell;
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Table table = new Table(1);
        Cell cell = getCell("The title of this cell is title 1", "title 1");
        table.addCell(cell);
        cell = getCell("The title of this cell is title 2", "title 2");
        table.addCell(cell);
        cell = getCell("The title of this cell is title 3", "title 3");
        table.addCell(cell);
        doc.add(table);
        doc.close();
    }
 
 
    protected class CellTitleRenderer extends CellRenderer {
        protected String title;
 
        public CellTitleRenderer(Cell modelElement, String title) {
            super(modelElement);
            this.title = title;
        }
 
        @Override
        public void drawBorder(DrawContext drawContext) {
            // create above canvas in order to draw above borders (notice that we draw borders using TableRenderer)
            PdfCanvas aboveCanvas = new PdfCanvas(drawContext.getDocument().getLastPage().newContentStreamAfter(),
                    drawContext.getDocument().getLastPage().getResources(), drawContext.getDocument());
            new Canvas(aboveCanvas, drawContext.getDocument(), getOccupiedAreaBBox())
                    .add(new Paragraph(title)
                            .setBackgroundColor(Color.LIGHT_GRAY)
                            .setFixedPosition(getOccupiedAreaBBox().getLeft() + 5, getOccupiedAreaBBox().getTop() - 8, 30));
        }
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TableRenderer;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class CustomBorder extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/custom_border.pdf";
    public static final String TEXT = "This is some long paragraph that will be added over and over " +
            "again to prove a point. It should result in rows that are split and rows that aren't.";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CustomBorder().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Table table = new Table(2);
        table.setWidth(500);
        table.setBorder(new SolidBorder(1));
        Cell cell = new Cell().add(new Paragraph(TEXT));
        cell.setBorder(null);
        for (int i = 0; i < 60; ) {
            table.addCell(new Cell().add(new Paragraph("Cell " + (++i))).setBorder(Border.NO_BORDER));
            table.addCell(new Cell().add(new Paragraph(TEXT)).setBorder(Border.NO_BORDER));
        }
        table.setNextRenderer(new CustomBorderTableRenderer(table, new Table.RowRange(0, 59)));
        doc.add(table);
 
        doc.close();
    }
}
 
 
class CustomBorderTableRenderer extends TableRenderer {
    protected static boolean isNextPageTopToBeDrawn = false;
    protected static boolean isTopToBeDrawn = true;
 
    public CustomBorderTableRenderer(Table modelElement, Table.RowRange rowRange) {
        super(modelElement, rowRange);
    }
 
    protected CustomBorderTableRenderer(Table modelElement) {
        super(modelElement);
    }
 
    @Override
    public void drawBorder(DrawContext drawContext) {
        CellRenderer[] firstRowRenderers = rows.get(0);
        // yLines
        PdfCanvas canvas = drawContext.getCanvas();
        for (CellRenderer cellRenderer : firstRowRenderers) {
            canvas.moveTo(cellRenderer.getBorderAreaBBox().getRight(),
                    getBorderAreaBBox().getBottom());
            canvas.lineTo(cellRenderer.getBorderAreaBBox().getRight(),
                    getBorderAreaBBox().getTop());
        }
        // the first yLine
        canvas.moveTo(firstRowRenderers[0].getBorderAreaBBox().getLeft(),
                getBorderAreaBBox().getBottom());
        canvas.lineTo(firstRowRenderers[0].getBorderAreaBBox().getLeft(),
                getBorderAreaBBox().getTop());
        canvas.stroke();
 
        for (CellRenderer[] cellRenderers : rows) {
            canvas.moveTo(cellRenderers[0].getBorderAreaBBox().getLeft(),
                    cellRenderers[0].getBorderAreaBBox().getBottom());
            canvas.lineTo(cellRenderers[cellRenderers.length - 1].getBorderAreaBBox().getRight(),
                    cellRenderers[cellRenderers.length - 1].getBorderAreaBBox().getBottom());
        }
        if (isTopToBeDrawn) {
            canvas.moveTo(firstRowRenderers[0].getBorderAreaBBox().getLeft(),
                    firstRowRenderers[0].getBorderAreaBBox().getTop());
            canvas.lineTo(firstRowRenderers[firstRowRenderers.length - 1].getBorderAreaBBox().getRight(),
                    firstRowRenderers[0].getBorderAreaBBox().getTop());
            isTopToBeDrawn = false;
        }
        if (isNextPageTopToBeDrawn) {
            isTopToBeDrawn = true;
            isNextPageTopToBeDrawn = false;
        }
        canvas.stroke();
    }
 
    @Override
    public CustomBorderTableRenderer getNextRenderer() {
        return new CustomBorderTableRenderer((Table) modelElement);
    }
 
    @Override
    protected TableRenderer[] split(int row) {
        if (null != rows.get(row)) {
            isNextPageTopToBeDrawn = true;
            CellRenderer[] curRow = rows.get(row);
            for (int i = 0; i < curRow.length; i++) {
                if (0 != rows.get(row)[0].getChildRenderers().size()) {
                    isNextPageTopToBeDrawn = false;
                    break;
                }
            }
        }
        CustomBorderTableRenderer splitRenderer = (CustomBorderTableRenderer) createSplitRenderer(
                new Table.RowRange(rowRange.getStartRow(), rowRange.getStartRow() + row));
        splitRenderer.rows = rows.subList(0, row);
        CustomBorderTableRenderer overflowRenderer = (CustomBorderTableRenderer) createOverflowRenderer(
                new Table.RowRange(rowRange.getStartRow() + row, rowRange.getFinishRow()));
        overflowRenderer.rows = rows.subList(row, rows.size());
        splitRenderer.occupiedArea = occupiedArea;
        return new TableRenderer[]{splitRenderer, overflowRenderer};
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TableRenderer;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class CustomBorder2 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/custom_border2.pdf";
    public static final String TEXT = "This is some long paragraph that will be added over and over " +
            "again to prove a point. It should result in rows that are split and rows that aren't.";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CustomBorder2().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(2);
        table.setWidth(500);
        table.setBorder(new SolidBorder(1));
 
 
        Cell cell = new Cell().add(new Paragraph(TEXT));
 
        cell.setBorder(null);
        for (int i = 0; i < 60; ) {
            table.addCell(new Cell().add("Cell " + (++i)).setBorder(Border.NO_BORDER));
            table.addCell(new Cell().add(TEXT).setBorder(Border.NO_BORDER));
        }
        table.setNextRenderer(new CustomBorder2TableRenderer(table, new Table.RowRange(0, 59)));
        doc.add(table);
 
        doc.close();
    }
}
 
 
class CustomBorder2TableRenderer extends TableRenderer {
    protected static boolean isBottomToBeDrawn = false;
    protected static boolean isTopToBeDrawn = true;
 
    public CustomBorder2TableRenderer(Table modelElement) {
        super(modelElement);
    }
 
    public CustomBorder2TableRenderer(Table modelElement, Table.RowRange rowRange) {
        super(modelElement, rowRange);
    }
 
    @Override
    public void drawBorder(DrawContext drawContext) {
        CellRenderer[] firstRowRenderers = rows.get(0);
        // yLines
        PdfCanvas canvas = drawContext.getCanvas();
        canvas.moveTo(firstRowRenderers[0].getBorderAreaBBox().getLeft(),
                getBorderAreaBBox().getBottom());
        canvas.lineTo(firstRowRenderers[0].getBorderAreaBBox().getLeft(),
                getBorderAreaBBox().getTop());
        canvas.moveTo(firstRowRenderers[firstRowRenderers.length - 1].getBorderAreaBBox().getRight(),
                getBorderAreaBBox().getBottom());
        canvas.lineTo(firstRowRenderers[firstRowRenderers.length - 1].getBorderAreaBBox().getRight(),
                getBorderAreaBBox().getTop());
 
        if (isTopToBeDrawn) {
            canvas.moveTo(firstRowRenderers[0].getBorderAreaBBox().getLeft(),
                    firstRowRenderers[0].getBorderAreaBBox().getTop());
            canvas.lineTo(firstRowRenderers[firstRowRenderers.length - 1].getBorderAreaBBox().getRight(),
                    firstRowRenderers[0].getBorderAreaBBox().getTop());
            isTopToBeDrawn = false;
        }
        if (isBottomToBeDrawn) {
            canvas.moveTo(rows.get(rows.size() - 1)[0].getBorderAreaBBox().getLeft(),
                    rows.get(rows.size() - 1)[0].getBorderAreaBBox().getBottom());
            canvas.lineTo(
                    rows.get(rows.size() - 1)[rows.get(rows.size() - 1).length - 1]
                            .getBorderAreaBBox().getRight(),
                    rows.get(rows.size() - 1)[rows.get(rows.size() - 1).length - 1]
                            .getBorderAreaBBox().getBottom());
 
        }
        canvas.stroke();
        isBottomToBeDrawn = true;
    }
 
    @Override
    public CustomBorder2TableRenderer getNextRenderer() {
        return new CustomBorder2TableRenderer((Table) modelElement);
    }
 
    @Override
    protected TableRenderer[] split(int row) {
        isBottomToBeDrawn = false;
        return super.split(row);
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34555756/one-cell-with-different-border-types
 */
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class CustomBorder3 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/custom_border_3.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CustomBorder3().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdfDoc);
 
        ILineDash solid = new Solid();
        ILineDash dotted = new Dotted();
        ILineDash dashed = new Dashed();
 
        Table table;
        Cell cell;
 
        table = new Table(4);
        table.setMarginBottom(30);
        cell = new Cell().add("dotted left border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new CustomBorder3Renderer(cell,
                new ILineDash[]{null, dotted, null, null}));
        table.addCell(cell);
        cell = new Cell().add("solid right border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new CustomBorder3Renderer(cell,
                new ILineDash[]{null, null, null, solid}));
        table.addCell(cell);
        cell = new Cell().add("dashed top border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new CustomBorder3Renderer(cell,
                new ILineDash[]{dashed, null, null, null}));
        table.addCell(cell);
        cell = new Cell().add("bottom border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new CustomBorder3Renderer(cell,
                new ILineDash[]{null, null, solid, null}));
        table.addCell(cell);
        document.add(table);
 
        table = new Table(4);
        table.setMarginBottom(30);
        cell = new Cell().add("dotted left and solid top border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new CustomBorder3Renderer(cell,
                new ILineDash[]{solid, dotted, null, null}));
        table.addCell(cell);
        cell = new Cell().add("dashed right and dashed bottom border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new CustomBorder3Renderer(cell,
                new ILineDash[]{null, null, dashed, dashed}));
        table.addCell(cell);
        cell = new Cell().add("no border");
        cell.setBorder(Border.NO_BORDER);
        table.addCell(cell);
        cell = new Cell().add("full solid border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new CustomBorder3Renderer(cell,
                new ILineDash[]{solid, solid, solid, solid}));
        table.addCell(cell);
        document.add(table);
        document.close();
    }
 
 
    interface ILineDash {
        void applyLineDash(PdfCanvas canvas);
    }
 
 
    class Solid implements ILineDash {
        public void applyLineDash(PdfCanvas canvas) {
        }
    }
 
 
    class Dotted implements ILineDash {
        public void applyLineDash(PdfCanvas canvas) {
            canvas.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
            canvas.setLineDash(0, 4, 2);
        }
    }
 
 
    class Dashed implements ILineDash {
        public void applyLineDash(PdfCanvas canvas) {
            canvas.setLineDash(3, 3);
        }
    }
 
 
    class CustomBorder3Renderer extends CellRenderer {
        ILineDash[] borders;
 
        public CustomBorder3Renderer(Cell modelElement, ILineDash[] borders) {
            super(modelElement);
            this.borders = new ILineDash[borders.length];
            for (int i = 0; i < this.borders.length; i++) {
                this.borders[i] = borders[i];
            }
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            PdfCanvas canvas = drawContext.getCanvas();
            Rectangle position = getOccupiedAreaBBox();
            canvas.saveState();
            if (null != borders[0]) {
                canvas.saveState();
                borders[0].applyLineDash(canvas);
                canvas.moveTo(position.getRight(), position.getTop());
                canvas.lineTo(position.getLeft(), position.getTop());
                canvas.stroke();
                canvas.restoreState();
            }
            if (null != borders[2]) {
                canvas.saveState();
                borders[2].applyLineDash(canvas);
                canvas.moveTo(position.getRight(), position.getBottom());
                canvas.lineTo(position.getLeft(), position.getBottom());
                canvas.stroke();
                canvas.restoreState();
            }
            if (null != borders[3]) {
                canvas.saveState();
                borders[3].applyLineDash(canvas);
                canvas.moveTo(position.getRight(), position.getTop());
                canvas.lineTo(position.getRight(), position.getBottom());
                canvas.stroke();
                canvas.restoreState();
            }
            if (null != borders[1]) {
                canvas.saveState();
                borders[1].applyLineDash(canvas);
                canvas.moveTo(position.getLeft(), position.getTop());
                canvas.lineTo(position.getLeft(), position.getBottom());
                canvas.stroke();
                canvas.restoreState();
            }
            canvas.stroke();
            canvas.restoreState();
        }
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TableRenderer;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class DottedLineCell extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/dotted_line_cell.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DottedLineCell().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        doc.add(new Paragraph("Table event"));
        Table table = new Table(3);
        table.setNextRenderer(new DottedLineTableRenderer(table, new Table.RowRange(0, 2)));
        table.addCell(new Cell().add("A1").setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("A2").setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("A3").setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("B1").setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("B2").setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("B3").setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("C1").setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("C2").setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("C3").setBorder(Border.NO_BORDER));
        doc.add(table);
        doc.add(new Paragraph("Cell event"));
        table = new Table(1);
        Cell cell = new Cell().add(new Paragraph("Test"));
        cell.setNextRenderer(new DottedLineCellRenderer(cell));
        cell.setBorder(Border.NO_BORDER);
        table.addCell(cell.setBorder(Border.NO_BORDER));
        doc.add(table);
 
        doc.close();
    }
 
 
    private class DottedLineTableRenderer extends TableRenderer {
        public DottedLineTableRenderer(Table modelElement, Table.RowRange rowRange) {
            super(modelElement, rowRange);
        }
 
        @Override
        public void drawChildren(DrawContext drawContext) {
            super.drawChildren(drawContext);
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.setLineDash(3f, 3f);
            // first horizontal line
            CellRenderer[] cellRenderers = rows.get(0);
            canvas.moveTo(cellRenderers[0].getOccupiedArea().getBBox().getLeft(),
                    cellRenderers[0].getOccupiedArea().getBBox().getTop());
            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.moveTo(cellRenderers[0].getOccupiedArea().getBBox().getX(),
                        cellRenderers[0].getOccupiedArea().getBBox().getY());
                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());
                }
            }
            canvas.stroke();
        }
    }
 
 
    private class DottedLineCellRenderer extends CellRenderer {
        public DottedLineCellRenderer(Cell modelElement) {
            super(modelElement);
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            drawContext.getCanvas().setLineDash(3f, 3f);
            drawContext.getCanvas().rectangle(this.getOccupiedArea().getBBox());
            drawContext.getCanvas().stroke();
        }
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34555756/one-cell-with-different-border-types
 */
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class DottedLineCell2 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/dotted_line_cell2.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DottedLineCell2().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdfDoc);
        Table table;
        Cell cell;
 
        table = new Table(4);
        table.setMarginBottom(30);
        cell = new Cell().add("left border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new DottedLineCellRenderer(cell,
                new boolean[]{false, true, false, false}));
        table.addCell(cell);
        cell = new Cell().add("right border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new DottedLineCellRenderer(cell,
                new boolean[]{false, false, false, true}));
        table.addCell(cell);
        cell = new Cell().add("top border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new DottedLineCellRenderer(cell,
                new boolean[]{true, false, false, false}));
        table.addCell(cell);
        cell = new Cell().add("bottom border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new DottedLineCellRenderer(cell,
                new boolean[]{false, false, true, false}));
        table.addCell(cell);
        document.add(table);
 
        table = new Table(4);
        table.setMarginBottom(30);
        cell = new Cell().add("left and top border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new DottedLineCellRenderer(cell,
                new boolean[]{true, true, false, false}));
        table.addCell(cell);
        cell = new Cell().add("right and bottom border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new DottedLineCellRenderer(cell,
                new boolean[]{false, false, true, true}));
        table.addCell(cell);
        cell = new Cell().add("no border");
        cell.setBorder(Border.NO_BORDER);
        table.addCell(cell);
        cell = new Cell().add("full border");
        cell.setBorder(Border.NO_BORDER);
        cell.setNextRenderer(new DottedLineCellRenderer(cell,
                new boolean[]{true, true, true, true}));
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}
 
 
class DottedLineCellRenderer extends CellRenderer {
    boolean[] borders;
 
    public DottedLineCellRenderer(Cell modelElement, boolean[] borders) {
        super(modelElement);
        this.borders = new boolean[borders.length];
        for (int i = 0; i < this.borders.length; i++) {
            this.borders[i] = borders[i];
        }
    }
 
    @Override
    public void draw(DrawContext drawContext) {
        super.draw(drawContext);
        PdfCanvas canvas = drawContext.getCanvas();
        Rectangle position = getOccupiedAreaBBox();
        canvas.saveState();
        canvas.setLineDash(0, 4, 2);
        if (borders[0]) {
            canvas.moveTo(position.getRight(), position.getTop());
            canvas.lineTo(position.getLeft(), position.getTop());
        }
        if (borders[2]) {
            canvas.moveTo(position.getRight(), position.getBottom());
            canvas.lineTo(position.getLeft(), position.getBottom());
        }
        if (borders[3]) {
            canvas.moveTo(position.getRight(), position.getTop());
            canvas.lineTo(position.getRight(), position.getBottom());
        }
        if (borders[1]) {
            canvas.moveTo(position.getLeft(), position.getTop());
            canvas.lineTo(position.getLeft(), position.getBottom());
        }
        canvas.stroke();
        canvas.restoreState();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TableRenderer;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class DottedLineHeader extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/dotted_line_header.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new DottedLineHeader().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(3);
        table.setNextRenderer(new DottedHeaderTableRenderer(table, new Table.RowRange(0, 1)));
        Style noBorder = new Style().setBorder(Border.NO_BORDER);
        table.addHeaderCell(new Cell().add("A1").addStyle(noBorder));
        table.addHeaderCell(new Cell().add("A2").addStyle(noBorder));
        table.addHeaderCell(new Cell().add("A3").addStyle(noBorder));
        table.addCell(new Cell().add("B1").addStyle(noBorder));
        table.addCell(new Cell().add("B2").addStyle(noBorder));
        table.addCell(new Cell().add("B3").addStyle(noBorder));
        table.addCell(new Cell().add("C1").addStyle(noBorder));
        table.addCell(new Cell().add("C2").addStyle(noBorder));
        table.addCell(new Cell().add("C3").addStyle(noBorder));
        doc.add(table);
        doc.add(new Paragraph("Cell event"));
        table = new Table(3);
        Cell cell = new Cell().add(new Paragraph("A1")).addStyle(noBorder);
        cell.setNextRenderer(new DottedHeaderCellRenderer(cell));
        table.addCell(cell);
        cell = new Cell().add(new Paragraph("A2")).addStyle(noBorder);
        cell.setNextRenderer(new DottedHeaderCellRenderer(cell));
        table.addCell(cell);
        cell = new Cell().add(new Paragraph("A3")).addStyle(noBorder);
        cell.setNextRenderer(new DottedHeaderCellRenderer(cell));
        table.addCell(cell);
        table.addCell(new Cell().add("B1").addStyle(noBorder));
        table.addCell(new Cell().add("B2").addStyle(noBorder));
        table.addCell(new Cell().add("B3").addStyle(noBorder));
        table.addCell(new Cell().add("C1").addStyle(noBorder));
        table.addCell(new Cell().add("C2").addStyle(noBorder));
        table.addCell(new Cell().add("C3").addStyle(noBorder));
        doc.add(table);
 
        doc.close();
    }
 
 
    private class DottedHeaderTableRenderer extends TableRenderer {
        public DottedHeaderTableRenderer(Table modelElement, Table.RowRange rowRange) {
            super(modelElement, rowRange);
        }
 
        @Override
        public void drawChildren(DrawContext drawContext) {
            super.drawChildren(drawContext);
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.setLineDash(3f, 3f);
            Rectangle headersArea = headerRenderer.getOccupiedArea().getBBox();
            canvas.moveTo(headersArea.getLeft(), headersArea.getTop());
            canvas.lineTo(headersArea.getRight(), headersArea.getTop());
 
            canvas.moveTo(headersArea.getLeft(), headersArea.getBottom());
            canvas.lineTo(headersArea.getRight(), headersArea.getBottom());
            canvas.stroke();
        }
    }
 
 
    private class DottedHeaderCellRenderer extends CellRenderer {
        public DottedHeaderCellRenderer(Cell modelElement) {
            super(modelElement);
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            super.draw(drawContext);
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.setLineDash(3f, 3f);
            canvas.moveTo(this.getOccupiedArea().getBBox().getLeft(), this.getOccupiedArea().getBBox().getBottom());
            canvas.lineTo(this.getOccupiedArea().getBBox().getRight(),
                    this.getOccupiedArea().getBBox().getBottom());
            canvas.moveTo(this.getOccupiedArea().getBBox().getLeft(),
                    this.getOccupiedArea().getBBox().getTop());
            canvas.lineTo(this.getOccupiedArea().getBBox().getRight(),
                    this.getOccupiedArea().getBBox().getTop());
            canvas.stroke();
        }
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.CellRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class RoundedCorners extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/rounded_corners.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RoundedCorners().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(3);
        Cell cell = getCell("These cells have rounded borders at the top.");
        table.addCell(cell);
        cell = getCell("These cells aren't rounded at the bottom.");
        table.addCell(cell);
        cell = getCell("A custom cell event was used to achieve this.");
        table.addCell(cell);
        doc.add(table);
 
        doc.close();
    }
 
    public Cell getCell(String content) {
        Cell cell = new Cell().add(new Paragraph(content));
        cell.setNextRenderer(new RoundedCornersCellRenderer(cell));
        cell.setPadding(5);
        cell.setBorder(null);
        return cell;
    }
 
 
    private class RoundedCornersCellRenderer extends CellRenderer {
        public RoundedCornersCellRenderer(Cell modelElement) {
            super(modelElement);
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            float llx = getOccupiedAreaBBox().getX() + 2;
            float lly = getOccupiedAreaBBox().getY() + 2;
            float urx = getOccupiedAreaBBox().getX() + getOccupiedAreaBBox().getWidth() - 2;
            float ury = getOccupiedAreaBBox().getY() + getOccupiedAreaBBox().getHeight() - 2;
            float r = 4;
            float b = 0.4477f;
            PdfCanvas canvas = drawContext.getCanvas();
            canvas.moveTo(llx, lly);
            canvas.lineTo(urx, lly);
            canvas.lineTo(urx, ury - r);
            canvas.curveTo(urx, ury - r * b, urx - r * b, ury, urx - r, ury);
            canvas.lineTo(llx + r, ury);
            canvas.curveTo(llx + r * b, ury, llx, ury - r * b, llx, ury - r);
            canvas.lineTo(llx, lly);
            canvas.stroke();
            super.draw(drawContext);
        }
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
* Example written by Bruno Lowagie in answer to the following question:
* http://stackoverflow.com/questions/35340003
*/
package com.itextpdf.samples.sandbox.tables;
 
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.TableRenderer;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class TableBorder extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/tables_border.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TableBorder().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Table table = new Table(4);
        for(int aw = 0; aw < 16; aw++){
            table.addCell(new Cell().add("hi").setBorder(Border.NO_BORDER));
        }
        // Notice that one should set renderer after table completion
        table.setNextRenderer(new TableBorderRenderer(table));
        doc.add(table);
        doc.close();
    }
 
 
    class TableBorderRenderer extends TableRenderer {
        public TableBorderRenderer(Table modelElement) {
            super(modelElement);
        }
 
        @Override
        protected void drawBorders(DrawContext drawContext) {
            Rectangle rect = getOccupiedAreaBBox();
            drawContext.getCanvas()
                    .saveState()
                    .rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight())
                    .stroke()
                    .restoreState();
        }
    }
}