See the iText website example:
/**
* Este ejemplo fue escrito por Bruno Lowagie en respuesta a las siguientes preguntas:
* 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("¡No es inteligente usar 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("¡Actualiza si eres un desarrollador profesional!");
}
table.addCell("21");
table.addCell(innertable);
for (int i = 22; i <= 40; i++) {
table.addCell(String.valueOf(i));
table.addCell("¡No es inteligente usar iText 2.1.7!");
}
document.add(table);
document.close();
}
}
This is creating nested tables, since setKeepTogether(True)
does not work in the cells, but in the table. When you reach 36, you can create the external table.