Skip to main content
Skip table of contents

Nested tables

These examples were written in answer to questions such as:


nestedtables

JAVA

JAVA
/**
 * This example was written by Bruno Lowagie in answer to the following questions:
 * http://stackoverflow.com/questions/24562448/the-table-width-must-be-greater-than-zero-exception-when-using-nested-tables
 * and
 * http://stackoverflow.com/questions/28444598/nested-table-stretches
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class NestedTables {
    
    public static final String DEST = "results/tables/nested_tables.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        float[] columnWidths = {183, 31, 88, 49, 35, 25, 35, 35, 35, 32, 32, 33, 35, 60, 46, 26 };
        PdfPTable table = new PdfPTable(columnWidths);
        table.setTotalWidth(770F);
        table.setLockedWidth(true);
        buildNestedTables(table);
        document.add(new Paragraph("Add table straight to another table"));
        document.add(table);
        table = new PdfPTable(columnWidths);
        table.setTotalWidth(770F);
        table.setLockedWidth(true);
        buildNestedTables2(table);
        document.add(new Paragraph("Add table to the cell constructor"));
        document.add(table);
        table = new PdfPTable(columnWidths);
        table.setTotalWidth(770F);
        table.setLockedWidth(true);
        buildNestedTables3(table);
        document.add(new Paragraph("Add table as an element to a cell"));
        document.add(table);
        document.close();
    }
    
    private void buildNestedTables(PdfPTable outerTable) {
        PdfPTable innerTable1 = new PdfPTable(1);
        PdfPTable innerTable2 = new PdfPTable(2);
        PdfPCell 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 PdfPCell();
        cell.setColspan(14);
        outerTable.addCell(cell);
   }
    
    private void buildNestedTables2(PdfPTable outerTable) {
        PdfPTable innerTable1 = new PdfPTable(1);
        innerTable1.setWidthPercentage(100);
        PdfPTable innerTable2 = new PdfPTable(2);
        innerTable2.setWidthPercentage(100);
        PdfPCell cell;
        innerTable1.addCell("Cell 1");
        innerTable1.addCell("Cell 2");
        cell = new PdfPCell(innerTable1);
        outerTable.addCell(cell);
        innerTable2.addCell("Cell 3");
        innerTable2.addCell("Cell 4");
        cell = new PdfPCell(innerTable2);
        outerTable.addCell(cell);
        cell = new PdfPCell();
        cell.setColspan(14);
        outerTable.addCell(cell);
   }
    
    private void buildNestedTables3(PdfPTable outerTable) {
        PdfPTable innerTable1 = new PdfPTable(1);
        innerTable1.setWidthPercentage(100);
        PdfPTable innerTable2 = new PdfPTable(2);
        innerTable2.setWidthPercentage(100);
        PdfPCell cell;
        innerTable1.addCell("Cell 1");
        innerTable1.addCell("Cell 2");
        cell = new PdfPCell();
        cell.addElement(innerTable1);
        outerTable.addCell(cell);
        innerTable2.addCell("Cell 3");
        innerTable2.addCell("Cell 4");
        cell = new PdfPCell();
        cell.addElement(innerTable2);
        outerTable.addCell(cell);
        cell = new PdfPCell();
        cell.setColspan(14);
        outerTable.addCell(cell);
   }
}


nestedtables2

JAVA

JAVA
/**
 * This example was written by Bruno Lowagie in answer to the following questions:
 * http://stackoverflow.com/questions/28503491/large-table-in-table-cell-invoke-page-break
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class NestedTables2 {
    public static final String DEST = "results/tables/nested_tables2.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables2().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setSplitLate(false);
        table.setWidths(new int[]{1, 15});
        for (int i = 1; i <= 20; i++) {
            table.addCell(String.valueOf(i));
            table.addCell("It is not smart to use iText 2.1.7!");
        }
        PdfPTable innertable = new PdfPTable(2);
        innertable.setWidths(new int[]{1, 15});
        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!");
        }
        document.add(table);
        document.close();
    }
}


nestedtables3

JAVA

JAVA
/*
 * This example was written by Bruno Lowagie in answer to the following questions:
 * http://stackoverflow.com/questions/31108488/pdfptable-header-repeat-when-data-in-a-different-table-increases-in-itext
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

/**
 * @author iText
 */
@WrapToTest
public class NestedTables3 {
    
    class MyPdfPTableEvent implements PdfPTableEvent {

        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        }

        public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
            ColumnText ct = new ColumnText(canvases[PdfPTable.TEXTCANVAS]);
            for (int i = 1; i < widths[1].length; i++) {
                Rectangle position = new Rectangle(widths[1][i - 1], heights[1], widths[1][i], heights[2]);
                ct.setSimpleColumn(position);
                ct.addText(new Phrase("This inner table header will always be repeated"));
                try {
                    ct.go();
                } catch (DocumentException ex) {
                }
            }
        }
    }
    
    public static final String DEST = "results/tables/nested_tables3.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables3().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setTableEvent(new MyPdfPTableEvent());
        PdfPCell cell = new PdfPCell(
            new Phrase("This outer header is repeated on every page"));
        cell.setColspan(2);
        table.addCell(cell);
        table.setHeaderRows(1);
        PdfPTable inner1 = new PdfPTable(1);
        cell = new PdfPCell();
        cell.setFixedHeight(20);
        inner1.addCell(cell);
        cell = new PdfPCell(
            new Phrase("This inner header won't be repeated on every page"));
        inner1.addCell(cell);
        inner1.setHeaderRows(2);
        for (int i = 0; i < 10; i++) {
            inner1.addCell("test");
        }
        cell = new PdfPCell(inner1);
        table.addCell(cell);
        PdfPTable inner2 = new PdfPTable(1);
        cell = new PdfPCell();
        cell.setFixedHeight(20);
        inner2.addCell(cell);
        cell = new PdfPCell(
            new Phrase("This inner may be repeated on every page"));
        inner2.addCell(cell);
        inner2.setHeaderRows(2);
        for (int i = 0; i < 35; i++) {
            inner2.addCell("test");
        }
        cell = new PdfPCell(inner2);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}


nestedtableproblem

JAVA

JAVA
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/28418108/itext-how-to-add-an-inner-table-surrounded-by-text-to-a-table
 */
package sandbox.tables;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class NestedTableProblem {

    public static final String DEST = "results/tables/nested_table_problem.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTableProblem().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
    	Document document = new Document(PageSize.LETTER, 21, 21, 30, 35);
    	PdfWriter.getInstance(document, new FileOutputStream(dest));
    	document.open();
    	// table 2
        final PdfPTable table2 = new PdfPTable(1);
    	table2.setHorizontalAlignment(Element.ALIGN_LEFT);
    	table2.getDefaultCell().setBorderColor(BaseColor.RED);
    	table2.getDefaultCell().setBorderWidth(1);
    	table2.addCell("Goodbye World");
      	// table 1
    	final PdfPTable table1 = new PdfPTable(1);
       	table1.setHorizontalAlignment(Element.ALIGN_LEFT);
    	table1.setWidthPercentage(100);
    	// contents
        PdfPCell cell = new PdfPCell();
    	cell.setBorderColor(BaseColor.BLACK);
    	cell.setBorderWidth(1);
        cell.addElement(new Chunk("Hello World"));
    	cell.addElement(table2);
    	cell.addElement(new Chunk("Hello World"));
    	table1.addCell(cell);
    	document.add(table1);
    	document.close();
    }
}


nestedtablesaligned

JAVA

JAVA
/**
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/26625455/unable-to-left-align-nested-tables-inside-a-cell
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class NestedTablesAligned {
    
    public static final String DEST = "results/tables/nested_tables_aligned.pdf";
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTablesAligned().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        float[] columnWidths = {200f, 200f, 200f};
        PdfPTable table = new PdfPTable(columnWidths);
        table.setTotalWidth(600f);
        table.setLockedWidth(true);
        buildNestedTables(table);
        document.add(table);
        document.close();
    }
    
    private void buildNestedTables(PdfPTable outerTable) {
        PdfPTable innerTable1 = new PdfPTable(1);
        innerTable1.setTotalWidth(100f);
        innerTable1.setLockedWidth(true);
        innerTable1.setHorizontalAlignment(Element.ALIGN_LEFT);
        innerTable1.addCell("Cell 1");
        innerTable1.addCell("Cell 2");
        outerTable.addCell(innerTable1);
        PdfPTable innerTable2 = new PdfPTable(2);
        innerTable2.setTotalWidth(100f);
        innerTable2.setLockedWidth(true);
        innerTable2.setHorizontalAlignment(Element.ALIGN_CENTER);
        innerTable2.addCell("Cell 3");
        innerTable2.addCell("Cell 4");
        outerTable.addCell(innerTable2);
        PdfPTable innerTable3 = new PdfPTable(2);
        innerTable3.setTotalWidth(100f);
        innerTable3.setLockedWidth(true);
        innerTable3.setHorizontalAlignment(Element.ALIGN_RIGHT);
        innerTable3.addCell("Cell 5");
        innerTable3.addCell("Cell 6");
        outerTable.addCell(innerTable3);
   }
}


nestedtableroundedborder

JAVA

JAVA
/*
 * This example was written in answer to the following question:
 * http://stackoverflow.com/questions/31330062/need-to-make-pdf-sample-with-boxes-as-table-columns-by-android-app
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

/**
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class NestedTableRoundedBorder {
    class RoundRectangle implements PdfPCellEvent {
        public void cellLayout(PdfPCell cell, Rectangle rect,
                PdfContentByte[] canvas) {
            PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
            cb.roundRectangle(
                rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3,
                rect.getHeight() - 3, 4);
            cb.stroke();
        }
    }
    
    public static final String DEST = "results/tables/nested_table_rounded_border.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTableRoundedBorder().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPCell cell;
        PdfPCellEvent roundRectangle = new RoundRectangle();
        // outer table
        PdfPTable outertable = new PdfPTable(1);
        // inner table 1
        PdfPTable innertable = new PdfPTable(5);
        innertable.setWidths(new int[]{8, 12, 1, 4, 12});
        // first row
        // column 1
        cell = new PdfPCell(new Phrase("Record Ref:"));
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // column 2
        cell = new PdfPCell(new Phrase("GN Staff"));
        cell.setPaddingLeft(2);
        innertable.addCell(cell);
        // column 3
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // column 4
        cell = new PdfPCell(new Phrase("Date: "));
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // column 5
        cell = new PdfPCell(new Phrase("30/4/2015"));
        cell.setPaddingLeft(2);
        innertable.addCell(cell);
        // spacing
        cell = new PdfPCell();
        cell.setColspan(5);
        cell.setFixedHeight(3);
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // second row
        // column 1
        cell = new PdfPCell(new Phrase("Hospital:"));
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // column 2
        cell = new PdfPCell(new Phrase("Derby Royal"));
        cell.setPaddingLeft(2);
        innertable.addCell(cell);
        // column 3
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // column 4
        cell = new PdfPCell(new Phrase("Ward: "));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setPaddingLeft(5);
        innertable.addCell(cell);
        // column 5
        cell = new PdfPCell(new Phrase("21"));
        cell.setPaddingLeft(2);
        innertable.addCell(cell);
        // spacing
        cell = new PdfPCell();
        cell.setColspan(5);
        cell.setFixedHeight(3);
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // first nested table
        cell = new PdfPCell(innertable);
        cell.setCellEvent(roundRectangle);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setPadding(8);
        outertable.addCell(cell);
        // inner table 2
        innertable = new PdfPTable(4);
        innertable.setWidths(new int[]{3, 17, 1, 16});
        // first row
        // column 1
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // column 2
        cell = new PdfPCell(new Phrase("Name"));
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // column 3
        cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // column 4
        cell = new PdfPCell(new Phrase("Signature: "));
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // spacing
        cell = new PdfPCell();
        cell.setColspan(4);
        cell.setFixedHeight(3);
        cell.setBorder(Rectangle.NO_BORDER);
        innertable.addCell(cell);
        // subsequent rows
        for (int i = 1; i < 4; i++) {
            // column 1
            cell = new PdfPCell(new Phrase(String.format("%s:", i)));
            cell.setBorder(Rectangle.NO_BORDER);
            innertable.addCell(cell);
            // column 2
            cell = new PdfPCell();
            innertable.addCell(cell);
            // column 3
            cell = new PdfPCell();
            cell.setBorder(Rectangle.NO_BORDER);
            innertable.addCell(cell);
            // column 4
            cell = new PdfPCell();
            innertable.addCell(cell);
            // spacing
            cell = new PdfPCell();
            cell.setColspan(4);
            cell.setFixedHeight(3);
            cell.setBorder(Rectangle.NO_BORDER);
            innertable.addCell(cell);
        }
        // second nested table
        cell = new PdfPCell(innertable);
        cell.setCellEvent(roundRectangle);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setPadding(8);
        outertable.addCell(cell);
        // add the table
        document.add(outertable);
        document.close();
    }
}


nestedtables4

JAVA

JAVA
/*
 * This example was written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/36259214
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPTableEvent;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

/**
 *
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class NestedTables4 {
    
    public static final String DEST = "results/tables/nested_tables4.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables4().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate());
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(4);
        table.setTableEvent(new BorderEvent());
        table.setWidths(new int[]{1, 12, 8, 1});
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        // first row
        PdfPCell cell = new PdfPCell(new Phrase("Main table"));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(4);
        table.addCell(cell);
        // second row
        table.addCell("");
        table.addCell("nested table 1");
        table.addCell("nested table 2");
        table.addCell("");
        // third row
        // third row cell 1
        table.addCell("");
        // third row cell 2
        PdfPTable table1 = new PdfPTable(1);
        table1.addCell("cell 1 of nested table 1");
        table1.addCell("cell 2 of nested table 1");
        table1.addCell("cell 2 of nested table 1");
        table.addCell(new PdfPCell(table1));
        // third row cell 3
        PdfPTable table2 = new PdfPTable(2);
        table2.getDefaultCell().setMinimumHeight(10);
        table2.addCell("");
        table2.addCell("");
        cell = new PdfPCell(new Phrase("cell 2 of nested table 2"));
        cell.setColspan(2);
        table2.addCell(cell);
        cell = new PdfPCell(new Phrase("cell 3 of nested table 2"));
        cell.setColspan(2);
        table2.addCell(cell);
        table.addCell(new PdfPCell(table2));
        // third row cell 4
        table.addCell("");
        // fourth row
        cell = new PdfPCell();
        cell.setColspan(4);
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setMinimumHeight(16);
        table.addCell(cell);
        document.add(table);
        document.close();
    }
    
    public class BorderEvent implements PdfPTableEvent {
        public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
            float width[] = widths[0];
            float x1 = width[0];
            float x2 = width[width.length - 1];
            float y1 = heights[0];
            float y2 = heights[heights.length - 1];
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.rectangle(x1, y1, x2 - x1, y2 - y1);
            cb.stroke();
        }
    }
}


nestedtables5

JAVA

JAVA
/*
 * This example was written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/36259214
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

/**
 *
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class NestedTables5 {
    
    public static final String DEST = "results/tables/nested_tables5.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables5().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        // Header part
        PdfPTable table = new PdfPTable(2);
        table.setWidths(new int[]{50, 50});
        // first cell
        PdfPTable table1 = new PdfPTable(1);
        table1.getDefaultCell().setMinimumHeight(30);
        table1.addCell("Address 1");
        table1.addCell("Address 2");
        table1.addCell("Address 3");
        table.addCell(new PdfPCell(table1));
        // second cell
        PdfPTable table2 = new PdfPTable(2);
        table2.addCell("Date");
        table2.addCell("Place");
        PdfPCell cell = new PdfPCell(new Phrase("References"));
        cell.setMinimumHeight(40);
        cell.setColspan(2);
        table2.addCell(cell);
        cell = new PdfPCell(new Phrase("destination"));
        cell.setColspan(2);
        table2.addCell(cell);
        table.addCell(new PdfPCell(table2));
        // second row
        cell = new PdfPCell();
        cell.setColspan(2);
        cell.setMinimumHeight(16);
        table.addCell(cell);
        document.add(table);
        // Body part
        table = new PdfPTable(6);
        table.setWidths(new int[]{ 1, 2, 6, 1, 2, 2 });
        table.addCell("sl no");
        table.addCell("qty");
        table.addCell("Product");
        table.addCell("units");
        table.addCell("rate");
        table.addCell("total");
        table.setHeaderRows(1);
        for (int i = 0; i < 6; ) {
            table.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT);
            table.getDefaultCell().setMinimumHeight(16);
            table.addCell(String.valueOf(++i));
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
        }
        table.getDefaultCell().setFixedHeight(3);
        table.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
        table.addCell("");
        table.addCell("");
        table.addCell("");
        table.addCell("");
        table.addCell("");
        table.addCell("");
        document.add(table);
        document.close();
    }
}


nestedtables6

JAVA

JAVA
/*
 * This example was written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/37548146
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

/**
 *
 * @author Bruno Lowagie (iText Software)
 */
@WrapToTest
public class NestedTables6 {
    
    public static final String DEST = "results/tables/nested_tables6.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NestedTables6().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document(new Rectangle(1200, 800));
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        // Header part
        PdfPTable mainTable = new PdfPTable(1);
        mainTable.getDefaultCell().setPadding(0);
        mainTable.setTotalWidth(1000);
        mainTable.setLockedWidth(true);
        PdfPTable subTable1 = new PdfPTable(5);
        subTable1.setTotalWidth(new float[]{200, 200, 200, 100, 300});
        subTable1.setLockedWidth(true);
        subTable1.addCell("test 1");
        subTable1.addCell("test 2");
        subTable1.addCell("test 3");
        subTable1.addCell("test 4");
        subTable1.addCell("test 5");
        mainTable.addCell(subTable1);
        PdfPTable subTable2 = new PdfPTable(5);
        subTable2.setTotalWidth(new float[]{200, 100, 200, 200, 300});
        subTable2.setLockedWidth(true);
        subTable2.addCell("test 1");
        subTable2.addCell("test 2");
        subTable2.addCell("test 3");
        subTable2.addCell("test 4");
        subTable2.addCell("test 5");
        PdfPCell cell2 = new PdfPCell(subTable2);
        mainTable.addCell(cell2);
        PdfPTable subTable3 = new PdfPTable(5);
        subTable3.setTotalWidth(new float[]{200, 200, 100, 200, 300});
        subTable3.setLockedWidth(true);
        subTable3.addCell("test 1");
        subTable3.addCell("test 2");
        subTable3.addCell("test 3");
        subTable3.addCell("test 4");
        subTable3.addCell("test 5");
        PdfPCell cell3 = new PdfPCell();
        cell3.setPadding(0);
        cell3.addElement(subTable3);
        mainTable.addCell(cell3);
        document.add(mainTable);
        document.close();
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.