Nested tables

Tags: nested tablestablesiText 7
Files: 
/*
 
    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.color.Color;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
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 NestedTableProblem extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/nested_table_problem.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTableProblem().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(612, 792));
        doc.setMargins(30, 21, 35, 21);
 
        // table 2
        Table table2 = new Table(1);
        table2.setHorizontalAlignment(HorizontalAlignment.LEFT);
        table2.addCell(new Cell().setBorder(new SolidBorder(Color.RED, 1)).add("Goodbye World"));
        table2.setWidthPercent(80);
        // table 1
        Table table1 = new Table(1);
        table1.setHorizontalAlignment(HorizontalAlignment.LEFT);
        Cell cell = new Cell();
        cell.setBorder(new SolidBorder(Color.BLACK, 1));
        cell.add("Hello World");
        cell.add(table2);
        cell.add("Hello World");
        table1.addCell(cell);
        doc.add(table1);
 
        doc.close();
    }
}
/*
 
    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.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 org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class NestedTableRoundedBorder extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/nested_table_rounded_border.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTableRoundedBorder().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Cell cell;
        // outer table
        Table outertable = new Table(1);
        // inner table 1
        Table innertable = new Table(new float[]{8, 12, 1, 4, 12});
        innertable.setWidthPercent(100);
        // first row
        // column 1
        cell = new Cell().add("Record Ref:");
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // column 2
        cell = new Cell().add("GN Staff");
        cell.setPaddingLeft(2);
        innertable.addCell(cell);
        // column 3
        cell = new Cell();
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // column 4
        cell = new Cell().add("Date: ");
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // column 5
        cell = new Cell().add("30/4/2015");
        cell.setPaddingLeft(2);
        innertable.addCell(cell);
        // spacing
        cell = new Cell(1, 5);
        cell.setHeight(3);
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // second row
        // column 1
        cell = new Cell().add("Hospital:");
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // column 2
        cell = new Cell().add("Derby Royal");
        cell.setPaddingLeft(2);
        innertable.addCell(cell);
        // column 3
        cell = new Cell();
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // column 4
        cell = new Cell().add("Ward: ");
        cell.setBorder(Border.NO_BORDER);
        cell.setPaddingLeft(5);
        innertable.addCell(cell);
        // column 5
        cell = new Cell().add("21");
        cell.setPaddingLeft(2);
        innertable.addCell(cell);
        // spacing
        cell = new Cell(1, 5);
        cell.setHeight(3);
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // first nested table
        cell = new Cell().add(innertable);
        cell.setNextRenderer(new RoundedBorderCellRenderer(cell));
        cell.setBorder(Border.NO_BORDER);
        cell.setPadding(8);
        outertable.addCell(cell);
        // inner table 2
        innertable = new Table(new float[]{3, 17, 1, 16});
        innertable.setWidthPercent(100);
        // first row
        // column 1
        cell = new Cell();
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // column 2
        cell = new Cell().add("Name");
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // column 3
        cell = new Cell();
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // column 4
        cell = new Cell().add("Signature: ");
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // spacing
        cell = new Cell(1, 4);
        cell.setHeight(3);
        cell.setBorder(Border.NO_BORDER);
        innertable.addCell(cell);
        // subsequent rows
        for (int i = 1; i < 4; i++) {
            // column 1
            cell = new Cell().add(String.format("%s:", i));
            cell.setBorder(Border.NO_BORDER);
            innertable.addCell(cell);
            // column 2
            cell = new Cell();
            innertable.addCell(cell);
            // column 3
            cell = new Cell();
            cell.setBorder(Border.NO_BORDER);
            innertable.addCell(cell);
            // column 4
            cell = new Cell();
            innertable.addCell(cell);
            // spacing
            cell = new Cell(1, 4);
            cell.setHeight(3);
            cell.setBorder(Border.NO_BORDER);
            innertable.addCell(cell);
        }
        // second nested table
        cell = new Cell().add(innertable);
        cell.setNextRenderer(new RoundedBorderCellRenderer(cell));
        cell.setBorder(Border.NO_BORDER);
        cell.setPaddingLeft(8);
        cell.setPaddingTop(8);
        cell.setPaddingRight(8);
        cell.setPaddingBottom(8);
        outertable.addCell(cell);
        // add the table
        doc.add(outertable);
 
        doc.close();
    }
 
 
    private class RoundedBorderCellRenderer extends CellRenderer {
        public RoundedBorderCellRenderer(Cell modelElement) {
            super(modelElement);
        }
 
        @Override
        public void draw(DrawContext drawContext) {
            drawContext.getCanvas().roundRectangle(getOccupiedAreaBBox().getX() + 1.5f, getOccupiedAreaBBox().getY() + 1.5f,
                    getOccupiedAreaBBox().getWidth() - 3, getOccupiedAreaBBox().getHeight() - 3, 4);
            drawContext.getCanvas().stroke();
            super.draw(drawContext);
        }
    }
}
/*
 
    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.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
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.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class NestedTables extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/nested_tables.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        // Note that it is not necessary to create new PageSize object,
        // but for testing reasons (connected to parallelization) we call constructor here
        Document doc = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
 
        float[] columnWidths = {150, 40, 90, 51, 35, 25, 35, 35, 35, 32, 32, 33, 35, 60, 46, 26};
        Table table = new Table(columnWidths);
        table.setWidth(770F);
        buildNestedTables(table);
        doc.add(new Paragraph("Add table straight to another table"));
        doc.add(table);
 
        // IMPORTANT!!!
        // Two other examples (with methods buildNestedTables1 and buildNestedTables2)
        // make no sense in itext7 because there is only one way of adding cells to tables
        // in itext7. Please, check NestedTables.java in itext5.
 
        doc.close();
    }
 
    private void buildNestedTables(Table outerTable) {
        Table innerTable1 = new Table(1);
        Table innerTable2 = new Table(2);
        Cell cell;
        innerTable1.addCell("Cell 1");
        innerTable1.addCell("Cell 2");
        outerTable.addCell(innerTable1);
        innerTable2.addCell("Cell 3");
        innerTable2.addCell("Cell 4");
        outerTable.addCell(innerTable2);
        cell = new Cell(1, 14);
        outerTable.addCell(cell);
    }
}
/*
 
    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.layout.Document;
import com.itextpdf.layout.element.Table;
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 NestedTables2 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/nested_tables2.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables2().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(new float[]{1, 15});
        table.setWidthPercent(100);
        for (int i = 1; i <= 20; i++) {
            table.addCell(String.valueOf(i));
            table.addCell("It is not smart to use iText 2.1.7!");
        }
        Table innertable = new Table(new float[]{1, 15});
        innertable.setWidthPercent(100);
        for (int i = 0; i < 90; i++) {
            innertable.addCell(String.valueOf(i + 1));
            innertable.addCell("Upgrade if you're a professional developer!");
        }
        table.addCell("21");
        table.addCell(innertable);
        for (int i = 22; i <= 40; i++) {
            table.addCell(String.valueOf(i));
            table.addCell("It is not smart to use iText 2.1.7!");
        }
        doc.add(table);
 
        doc.close();
    }
}
/*
 
    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.PageSize;
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.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.Property;
import com.itextpdf.layout.renderer.AbstractRenderer;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
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 NestedTables3 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/nested_tables3.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables3().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        // Note that it is not necessary to create new PageSize object,
        // but for testing reasons (connected to parallelization) we call constructor here
        Document doc = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
 
        Table table = new Table(2);
        table.setNextRenderer(new InnerTableRenderer(table, new Table.RowRange(0, 0)));
        Cell cell = new Cell(1,2).add("This outer header is repeated on every page");
        table.addHeaderCell(cell);
        Table inner1 = new Table(1);
        cell = new Cell();
        cell.setHeight(20);
        inner1.addHeaderCell(cell);
        cell = new Cell().add("This inner header won't be repeated on every page");
        inner1.addHeaderCell(cell);
        for (int i = 0; i < 10; i++) {
            inner1.addCell(new Cell().add(new Paragraph("test")));
        }
        cell = new Cell().add(inner1);
        table.addCell(cell.setPadding(0));
        Table inner2 = new Table(1);
        cell = new Cell();
        cell.setHeight(20);
        inner2.addHeaderCell(cell);
        cell = new Cell().add("This inner may be repeated on every page");
        inner2.addHeaderCell(cell);
        for (int i = 0; i < 35; i++) {
            inner2.addCell("test");
        }
        cell = new Cell().add(inner2);
        table.addCell(cell.setPadding(0));
        doc.add(table);
 
        doc.close();
    }
 
 
    private class InnerTableRenderer extends TableRenderer {
        public InnerTableRenderer(Table modelElement, Table.RowRange rowRange) {
            super(modelElement, rowRange);
        }
 
        protected InnerTableRenderer(Table modelElement) {
            super(modelElement);
        }
 
        @Override
        public void addChild(IRenderer renderer) {
            super.addChild(renderer);
        }
 
        @Override
        protected TableRenderer[] split(int row) {
            InnerTableRenderer splitRenderer = (InnerTableRenderer) createSplitRenderer(
                    new Table.RowRange(rowRange.getStartRow(), rowRange.getStartRow() + row));
            splitRenderer.rows = rows.subList(0, row);
            InnerTableRenderer overflowRenderer = (InnerTableRenderer) createOverflowRenderer(
                    new Table.RowRange(rowRange.getStartRow() + row,rowRange.getFinishRow()));
            overflowRenderer.rows = rows.subList(row, rows.size());
            splitRenderer.occupiedArea = occupiedArea;
 
            return new TableRenderer[] {splitRenderer, overflowRenderer};
        }
 
        @Override
        public void drawChildren(DrawContext drawContext) {
            super.drawChildren(drawContext);
            for (IRenderer renderer : childRenderers) {
                PdfCanvas canvas = drawContext.getCanvas();
                canvas.beginText();
                Rectangle box = ((AbstractRenderer)renderer).getInnerAreaBBox();
                canvas.moveText(box.getLeft(), box.getTop() - this.getPropertyAsFloat(Property.FONT_SIZE));
                canvas.setFontAndSize(this.getPropertyAsFont(Property.FONT),
                        this.getPropertyAsFloat(Property.FONT_SIZE));
                canvas.showText("This inner table header will always be repeated");
                canvas.endText();
                canvas.stroke();
            }
        }
 
        @Override
        public InnerTableRenderer getNextRenderer() {
            return new InnerTableRenderer((Table) modelElement);
        }
    }
}
/*
 
    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.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
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 NestedTablesAligned extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/nested_tables_aligned.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTablesAligned().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        // Note that it is not necessary to create new PageSize object,
        // but for testing reasons (connected to parallelization) we call constructor here
        Document doc = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
 
        float[] columnWidths = {200f, 200f, 200f};
        Table table = new Table(columnWidths);
        buildNestedTables(table);
        doc.add(table);
 
        doc.close();
    }
 
    private void buildNestedTables(Table outerTable) {
        Table innerTable1 = new Table(1);
        innerTable1.setWidth(100f);
        innerTable1.setHorizontalAlignment(HorizontalAlignment.LEFT);
        innerTable1.addCell("Cell 1");
        innerTable1.addCell("Cell 2");
        outerTable.addCell(innerTable1);
        Table innerTable2 = new Table(2);
        innerTable2.setWidth(100f);
        innerTable2.setHorizontalAlignment(HorizontalAlignment.CENTER);
        innerTable2.addCell("Cell 3");
        innerTable2.addCell("Cell 4");
        outerTable.addCell(innerTable2);
        Table innerTable3 = new Table(2);
        innerTable3.setWidth(100f);
        innerTable3.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        innerTable3.addCell("Cell 5");
        innerTable3.addCell("Cell 6");
        outerTable.addCell(innerTable3);
    }
}