Splitting tables

Tags: tablessplit tablecelliText 7

Examples that show what happens when a table doesn't fit the page:

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.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.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 SplitRowAtEndOfPage extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/split_row_at_end_of_page.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SplitRowAtEndOfPage().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        Table table = new Table(1);
        table.setWidth(550);
        // the number of iterations has been changed in order to provide the same as in itext5 example
        for (int i = 0; i < 6; i++) {
            Cell cell;
            if (i == 5) {
                cell = new Cell().add("Three\nLines\nHere");
            } else {
                cell = new Cell().add(Integer.toString(i));
            }
            table.addCell(cell);
        }
 
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(612, 237));
        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.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
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 SplitRowAtSpecificRow extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/split_row_at_specific_row.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SplitRowAtSpecificRow().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        Table table = new Table(1);
        table.setWidth(550);
        // the number of iterations has been changed in order to provide the same as in itext5 example
        for (int i = 0; i < 6; i++) {
            Cell cell;
            if (i == 5) {
                cell = new Cell().add("Three\nLines\nHere");
            }
            else {
                cell = new Cell().add(Integer.toString(i));
            }
            table.addCell(cell);
        }
 
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(612, 237));
        doc.add(table);
        doc.close();
    }
}
/*
 
    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/33286841/stop-itext-table-from-spliting-on-new-page
 */
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.Paragraph;
import com.itextpdf.layout.element.Table;
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 Splitting extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/splitting.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Splitting().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Paragraph p = new Paragraph("Test");
        Table table = new Table(2);
        for (int i = 1; i < 6; i++) {
            table.addCell("key " + i);
            table.addCell("value " + i);
        }
        for (int i = 0; i < 27; i++) {
            doc.add(p);
        }
        doc.add(table);
        for (int i = 0; i < 23; i++) {
            doc.add(p);
        }
        table = new Table(2);
        for (int i = 1; i < 6; i++) {
            table.addCell("key " + i);
            table.addCell("value " + i);
        }
 
        Table nesting = new Table(1);
        Cell cell = new Cell().add(table);
        cell.setBorder(Border.NO_BORDER);
        nesting.addCell(cell.setKeepTogether(true));
        doc.add(nesting);
        doc.close();
    }
}
/*
 
    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/33286841/stop-itext-table-from-spliting-on-new-page
 */
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.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.Ignore;
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class Splitting2 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/splitting2.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new Splitting2().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        Paragraph p = new Paragraph("Test");
        Table table = new Table(2);
        for (int i = 1; i < 6; i++) {
            table.addCell("key " + i);
            table.addCell("value " + i);
        }
        for (int i = 0; i < 27; i++) {
            doc.add(p);
        }
        doc.add(table);
        for (int i = 0; i < 23; i++) {
            doc.add(p);
        }
        table = new Table(2);
        for (int i = 1; i < 6; i++) {
            table.addCell("key " + i);
            table.addCell("value " + i);
        }
        table.setKeepTogether(true);
        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.io.font.FontConstants;
import com.itextpdf.kernel.color.Color;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.border.SolidBorder;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.LineSeparator;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
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 TableSplitTest extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/tables/tables_split_test.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TableSplitTest().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(595, 842));
        doc.setMargins(55, 15, 35, 15);
 
        ILineDrawer line = new SolidLine(2);
        line.setColor(Color.LIGHT_GRAY);
        LineSeparator tableEndSeparator = new LineSeparator(line);
 
        tableEndSeparator.setMarginTop(10);
 
        String[] header = new String[]{"Header1", "Header2", "Header3",
                "Header4", "Header5"};
        String[] content = new String[]{"column 1", "column 2",
                "some Text in column 3", "Test data ", "column 5"};
 
        Table table = new Table(new float[]{3, 2, 4, 3, 2});
        table.setWidthPercent(98);
 
        for (String columnHeader : header) {
            Cell headerCell = new Cell().add(new Paragraph(columnHeader).setFont(
                    PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD))
                    .setFontSize(10));
            headerCell.setTextAlignment(TextAlignment.CENTER);
            headerCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
            headerCell.setBorder(new SolidBorder(Color.LIGHT_GRAY, 1));
            headerCell.setPadding(8);
            table.addHeaderCell(headerCell);
        }
        for (int i = 0; i < 15; i++) {
            int j = 0;
            for (String text : content) {
                if (i == 13 && j == 3) {
                    text = "Test data \n Test data \n Test data";
                }
                j++;
                Cell cell = new Cell().add(new Paragraph(text).setFont(
                        PdfFontFactory.createFont(FontConstants.HELVETICA))
                        .setFontSize(10));
                cell.setBorder(new SolidBorder(Color.LIGHT_GRAY, 1));
                cell.setPaddingLeft(5);
                cell.setPaddingTop(5);
                cell.setPaddingRight(5);
                cell.setPaddingBottom(5);
                table.addCell(cell);
            }
        }
        doc.add(table);
        doc.add(tableEndSeparator);
        for (int k = 0; k < 5; k++) {
            Paragraph info = new Paragraph("Some title").setFont(
                    PdfFontFactory.createFont(FontConstants.HELVETICA))
                    .setFontSize(10);
            info.setMarginTop(12f);
            doc.add(info);
            table = new Table(new float[]{3, 2, 4, 3, 2});
            table.setWidthPercent(98);
            table.setMarginTop(15);
 
            for (String columnHeader : header) {
                Cell headerCell = new Cell().add(new Paragraph(columnHeader).setFont(
                        PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD))
                        .setFontSize(10));
                headerCell.setTextAlignment(TextAlignment.CENTER);
                headerCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
                headerCell.setBorder(new SolidBorder(Color.LIGHT_GRAY, 1));
                headerCell.setPaddingLeft(8);
                headerCell.setPaddingTop(8);
                headerCell.setPaddingRight(8);
                headerCell.setPaddingBottom(8);
                table.addHeaderCell(headerCell);
            }
            for (String text : content) {
                Cell cell = new Cell().add(new Paragraph(text).setFont(
                        PdfFontFactory.createFont(FontConstants.HELVETICA))
                        .setFontSize(10));
                cell.setBorder(new SolidBorder(Color.LIGHT_GRAY, 1));
                cell.setPaddingLeft(5);
                cell.setPaddingTop(5);
                cell.setPaddingRight(5);
                cell.setPaddingBottom(5);
                table.addCell(cell);
            }
            doc.add(table);
            doc.add(tableEndSeparator);
        }
 
        doc.close();
    }
}