Nested tables

This is a code example of iText PDF, discover more.

5th November 2015
admin-marketing

Switch code for this example

NestedTables.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following questions:
  3.  * http://stackoverflow.com/questions/24562448/the-table-width-must-be-greater-than-zero-exception-when-using-nested-tables
  4.  * and
  5.  * http://stackoverflow.com/questions/28444598/nested-table-stretches
  6.  */
  7. package sandbox.tables;
  8.  
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.PageSize;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class NestedTables {
  24.    
  25.     public static final String DEST = "results/tables/nested_tables.pdf";
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new NestedTables().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document(PageSize.A4.rotate());
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         float[] columnWidths = {183, 31, 88, 49, 35, 25, 35, 35, 35, 32, 32, 33, 35, 60, 46, 26 };
  37.         PdfPTable table = new PdfPTable(columnWidths);
  38.         table.setTotalWidth(770F);
  39.         table.setLockedWidth(true);
  40.         buildNestedTables(table);
  41.         document.add(new Paragraph("Add table straight to another table"));
  42.         document.add(table);
  43.         table = new PdfPTable(columnWidths);
  44.         table.setTotalWidth(770F);
  45.         table.setLockedWidth(true);
  46.         buildNestedTables2(table);
  47.         document.add(new Paragraph("Add table to the cell constructor"));
  48.         document.add(table);
  49.         table = new PdfPTable(columnWidths);
  50.         table.setTotalWidth(770F);
  51.         table.setLockedWidth(true);
  52.         buildNestedTables3(table);
  53.         document.add(new Paragraph("Add table as an element to a cell"));
  54.         document.add(table);
  55.         document.close();
  56.     }
  57.    
  58.     private void buildNestedTables(PdfPTable outerTable) {
  59.         PdfPTable innerTable1 = new PdfPTable(1);
  60.         PdfPTable innerTable2 = new PdfPTable(2);
  61.         PdfPCell cell;
  62.         innerTable1.addCell("Cell 1");
  63.         innerTable1.addCell("Cell 2");
  64.         outerTable.addCell(innerTable1);
  65.         innerTable2.addCell("Cell 3");
  66.         innerTable2.addCell("Cell 4");
  67.         outerTable.addCell(innerTable2);
  68.         cell = new PdfPCell();
  69.         cell.setColspan(14);
  70.         outerTable.addCell(cell);
  71.    }
  72.    
  73.     private void buildNestedTables2(PdfPTable outerTable) {
  74.         PdfPTable innerTable1 = new PdfPTable(1);
  75.         innerTable1.setWidthPercentage(100);
  76.         PdfPTable innerTable2 = new PdfPTable(2);
  77.         innerTable2.setWidthPercentage(100);
  78.         PdfPCell cell;
  79.         innerTable1.addCell("Cell 1");
  80.         innerTable1.addCell("Cell 2");
  81.         cell = new PdfPCell(innerTable1);
  82.         outerTable.addCell(cell);
  83.         innerTable2.addCell("Cell 3");
  84.         innerTable2.addCell("Cell 4");
  85.         cell = new PdfPCell(innerTable2);
  86.         outerTable.addCell(cell);
  87.         cell = new PdfPCell();
  88.         cell.setColspan(14);
  89.         outerTable.addCell(cell);
  90.    }
  91.    
  92.     private void buildNestedTables3(PdfPTable outerTable) {
  93.         PdfPTable innerTable1 = new PdfPTable(1);
  94.         innerTable1.setWidthPercentage(100);
  95.         PdfPTable innerTable2 = new PdfPTable(2);
  96.         innerTable2.setWidthPercentage(100);
  97.         PdfPCell cell;
  98.         innerTable1.addCell("Cell 1");
  99.         innerTable1.addCell("Cell 2");
  100.         cell = new PdfPCell();
  101.         cell.addElement(innerTable1);
  102.         outerTable.addCell(cell);
  103.         innerTable2.addCell("Cell 3");
  104.         innerTable2.addCell("Cell 4");
  105.         cell = new PdfPCell();
  106.         cell.addElement(innerTable2);
  107.         outerTable.addCell(cell);
  108.         cell = new PdfPCell();
  109.         cell.setColspan(14);
  110.         outerTable.addCell(cell);
  111.    }
  112. }
NestedTables2.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following questions:
  3.  * http://stackoverflow.com/questions/28503491/large-table-in-table-cell-invoke-page-break
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;
  16.  
  17. @WrapToTest
  18. public class NestedTables2 {
  19.     public static final String DEST = "results/tables/nested_tables2.pdf";
  20.     public static void main(String[] args) throws IOException, DocumentException {
  21.         File file = new File(DEST);
  22.         file.getParentFile().mkdirs();
  23.         new NestedTables2().createPdf(DEST);
  24.     }
  25.    
  26.     public void createPdf(String dest) throws IOException, DocumentException {
  27.         Document document = new Document();
  28.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  29.         document.open();
  30.         PdfPTable table = new PdfPTable(2);
  31.         table.setSplitLate(false);
  32.         table.setWidths(new int[]{1, 15});
  33.         for (int i = 1; i
NestedTables3.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following questions:
  3.  * http://stackoverflow.com/questions/31108488/pdfptable-header-repeat-when-data-in-a-different-table-increases-in-itext
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfPTableEvent;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. /**
  25.  * @author iText
  26.  */
  27. @WrapToTest
  28. public class NestedTables3 {
  29.    
  30.     class MyPdfPTableEvent implements PdfPTableEvent {
  31.  
  32.         public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
  33.         }
  34.  
  35.         public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
  36.             ColumnText ct = new ColumnText(canvases[PdfPTable.TEXTCANVAS]);
  37.             for (int i = 1; i < widths[1].length; i++) {
  38.                 Rectangle position = new Rectangle(widths[1][i - 1], heights[1], widths[1][i], heights[2]);
  39.                 ct.setSimpleColumn(position);
  40.                 ct.addText(new Phrase("This inner table header will always be repeated"));
  41.                 try {
  42.                     ct.go();
  43.                 } catch (DocumentException ex) {
  44.                 }
  45.             }
  46.         }
  47.     }
  48.    
  49.     public static final String DEST = "results/tables/nested_tables3.pdf";
  50.    
  51.     public static void main(String[] args) throws IOException, DocumentException {
  52.         File file = new File(DEST);
  53.         file.getParentFile().mkdirs();
  54.         new NestedTables3().createPdf(DEST);
  55.     }
  56.    
  57.     public void createPdf(String dest) throws IOException, DocumentException {
  58.         Document document = new Document(PageSize.A4.rotate());
  59.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  60.         document.open();
  61.         PdfPTable table = new PdfPTable(2);
  62.         table.setTableEvent(new MyPdfPTableEvent());
  63.         PdfPCell cell = new PdfPCell(
  64.             new Phrase("This outer header is repeated on every page"));
  65.         cell.setColspan(2);
  66.         table.addCell(cell);
  67.         table.setHeaderRows(1);
  68.         PdfPTable inner1 = new PdfPTable(1);
  69.         cell = new PdfPCell();
  70.         cell.setFixedHeight(20);
  71.         inner1.addCell(cell);
  72.         cell = new PdfPCell(
  73.             new Phrase("This inner header won't be repeated on every page"));
  74.         inner1.addCell(cell);
  75.         inner1.setHeaderRows(2);
  76.         for (int i = 0; i < 10; i++) {
  77.             inner1.addCell("test");
  78.         }
  79.         cell = new PdfPCell(inner1);
  80.         table.addCell(cell);
  81.         PdfPTable inner2 = new PdfPTable(1);
  82.         cell = new PdfPCell();
  83.         cell.setFixedHeight(20);
  84.         inner2.addCell(cell);
  85.         cell = new PdfPCell(
  86.             new Phrase("This inner may be repeated on every page"));
  87.         inner2.addCell(cell);
  88.         inner2.setHeaderRows(2);
  89.         for (int i = 0; i < 35; i++) {
  90.             inner2.addCell("test");
  91.         }
  92.         cell = new PdfPCell(inner2);
  93.         table.addCell(cell);
  94.         document.add(table);
  95.         document.close();
  96.     }
  97. }
NestedTableProblem.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28418108/itext-how-to-add-an-inner-table-surrounded-by-text-to-a-table
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Element;
  12. import com.itextpdf.text.PageSize;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class NestedTableProblem {
  24.  
  25.     public static final String DEST = "results/tables/nested_table_problem.pdf";
  26.  
  27.     public static void main(String[] args) throws IOException,
  28.             DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new NestedTableProblem().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document(PageSize.LETTER, 21, 21, 30, 35);
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         // table 2
  39.         final PdfPTable table2 = new PdfPTable(1);
  40.         table2.setHorizontalAlignment(Element.ALIGN_LEFT);
  41.         table2.getDefaultCell().setBorderColor(BaseColor.RED);
  42.         table2.getDefaultCell().setBorderWidth(1);
  43.         table2.addCell("Goodbye World");
  44.         // table 1
  45.         final PdfPTable table1 = new PdfPTable(1);
  46.         table1.setHorizontalAlignment(Element.ALIGN_LEFT);
  47.         table1.setWidthPercentage(100);
  48.         // contents
  49.         PdfPCell cell = new PdfPCell();
  50.         cell.setBorderColor(BaseColor.BLACK);
  51.         cell.setBorderWidth(1);
  52.         cell.addElement(new Chunk("Hello World"));
  53.         cell.addElement(table2);
  54.         cell.addElement(new Chunk("Hello World"));
  55.         table1.addCell(cell);
  56.         document.add(table1);
  57.         document.close();
  58.     }
  59. }
NestedTablesAligned.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26625455/unable-to-left-align-nested-tables-inside-a-cell
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Element;
  10. import com.itextpdf.text.PageSize;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class NestedTablesAligned {
  21.    
  22.     public static final String DEST = "results/tables/nested_tables_aligned.pdf";
  23.     public static void main(String[] args) throws IOException, DocumentException {
  24.         File file = new File(DEST);
  25.         file.getParentFile().mkdirs();
  26.         new NestedTablesAligned().createPdf(DEST);
  27.     }
  28.    
  29.     public void createPdf(String dest) throws IOException, DocumentException {
  30.         Document document = new Document(PageSize.A4.rotate());
  31.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         float[] columnWidths = {200f, 200f, 200f};
  34.         PdfPTable table = new PdfPTable(columnWidths);
  35.         table.setTotalWidth(600f);
  36.         table.setLockedWidth(true);
  37.         buildNestedTables(table);
  38.         document.add(table);
  39.         document.close();
  40.     }
  41.    
  42.     private void buildNestedTables(PdfPTable outerTable) {
  43.         PdfPTable innerTable1 = new PdfPTable(1);
  44.         innerTable1.setTotalWidth(100f);
  45.         innerTable1.setLockedWidth(true);
  46.         innerTable1.setHorizontalAlignment(Element.ALIGN_LEFT);
  47.         innerTable1.addCell("Cell 1");
  48.         innerTable1.addCell("Cell 2");
  49.         outerTable.addCell(innerTable1);
  50.         PdfPTable innerTable2 = new PdfPTable(2);
  51.         innerTable2.setTotalWidth(100f);
  52.         innerTable2.setLockedWidth(true);
  53.         innerTable2.setHorizontalAlignment(Element.ALIGN_CENTER);
  54.         innerTable2.addCell("Cell 3");
  55.         innerTable2.addCell("Cell 4");
  56.         outerTable.addCell(innerTable2);
  57.         PdfPTable innerTable3 = new PdfPTable(2);
  58.         innerTable3.setTotalWidth(100f);
  59.         innerTable3.setLockedWidth(true);
  60.         innerTable3.setHorizontalAlignment(Element.ALIGN_RIGHT);
  61.         innerTable3.addCell("Cell 5");
  62.         innerTable3.addCell("Cell 6");
  63.         outerTable.addCell(innerTable3);
  64.    }
  65. }
NestedTableRoundedBorder.java
  1. /*
  2.  * This example was written in answer to the following question:
  3.  * http://stackoverflow.com/questions/31330062/need-to-make-pdf-sample-with-boxes-as-table-columns-by-android-app
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPCellEvent;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. /**
  23.  * @author Bruno Lowagie (iText Software)
  24.  */
  25. @WrapToTest
  26. public class NestedTableRoundedBorder {
  27.     class RoundRectangle implements PdfPCellEvent {
  28.         public void cellLayout(PdfPCell cell, Rectangle rect,
  29.                 PdfContentByte[] canvas) {
  30.             PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
  31.             cb.roundRectangle(
  32.                 rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3,
  33.                 rect.getHeight() - 3, 4);
  34.             cb.stroke();
  35.         }
  36.     }
  37.    
  38.     public static final String DEST = "results/tables/nested_table_rounded_border.pdf";
  39.    
  40.     public static void main(String[] args) throws IOException, DocumentException {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new NestedTableRoundedBorder().createPdf(DEST);
  44.     }
  45.    
  46.     public void createPdf(String dest) throws IOException, DocumentException {
  47.         Document document = new Document();
  48.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  49.         document.open();
  50.         PdfPCell cell;
  51.         PdfPCellEvent roundRectangle = new RoundRectangle();
  52.         // outer table
  53.         PdfPTable outertable = new PdfPTable(1);
  54.         // inner table 1
  55.         PdfPTable innertable = new PdfPTable(5);
  56.         innertable.setWidths(new int[]{8, 12, 1, 4, 12});
  57.         // first row
  58.         // column 1
  59.         cell = new PdfPCell(new Phrase("Record Ref:"));
  60.         cell.setBorder(Rectangle.NO_BORDER);
  61.         innertable.addCell(cell);
  62.         // column 2
  63.         cell = new PdfPCell(new Phrase("GN Staff"));
  64.         cell.setPaddingLeft(2);
  65.         innertable.addCell(cell);
  66.         // column 3
  67.         cell = new PdfPCell();
  68.         cell.setBorder(Rectangle.NO_BORDER);
  69.         innertable.addCell(cell);
  70.         // column 4
  71.         cell = new PdfPCell(new Phrase("Date: "));
  72.         cell.setBorder(Rectangle.NO_BORDER);
  73.         innertable.addCell(cell);
  74.         // column 5
  75.         cell = new PdfPCell(new Phrase("30/4/2015"));
  76.         cell.setPaddingLeft(2);
  77.         innertable.addCell(cell);
  78.         // spacing
  79.         cell = new PdfPCell();
  80.         cell.setColspan(5);
  81.         cell.setFixedHeight(3);
  82.         cell.setBorder(Rectangle.NO_BORDER);
  83.         innertable.addCell(cell);
  84.         // second row
  85.         // column 1
  86.         cell = new PdfPCell(new Phrase("Hospital:"));
  87.         cell.setBorder(Rectangle.NO_BORDER);
  88.         innertable.addCell(cell);
  89.         // column 2
  90.         cell = new PdfPCell(new Phrase("Derby Royal"));
  91.         cell.setPaddingLeft(2);
  92.         innertable.addCell(cell);
  93.         // column 3
  94.         cell = new PdfPCell();
  95.         cell.setBorder(Rectangle.NO_BORDER);
  96.         innertable.addCell(cell);
  97.         // column 4
  98.         cell = new PdfPCell(new Phrase("Ward: "));
  99.         cell.setBorder(Rectangle.NO_BORDER);
  100.         cell.setPaddingLeft(5);
  101.         innertable.addCell(cell);
  102.         // column 5
  103.         cell = new PdfPCell(new Phrase("21"));
  104.         cell.setPaddingLeft(2);
  105.         innertable.addCell(cell);
  106.         // spacing
  107.         cell = new PdfPCell();
  108.         cell.setColspan(5);
  109.         cell.setFixedHeight(3);
  110.         cell.setBorder(Rectangle.NO_BORDER);
  111.         innertable.addCell(cell);
  112.         // first nested table
  113.         cell = new PdfPCell(innertable);
  114.         cell.setCellEvent(roundRectangle);
  115.         cell.setBorder(Rectangle.NO_BORDER);
  116.         cell.setPadding(8);
  117.         outertable.addCell(cell);
  118.         // inner table 2
  119.         innertable = new PdfPTable(4);
  120.         innertable.setWidths(new int[]{3, 17, 1, 16});
  121.         // first row
  122.         // column 1
  123.         cell = new PdfPCell();
  124.         cell.setBorder(Rectangle.NO_BORDER);
  125.         innertable.addCell(cell);
  126.         // column 2
  127.         cell = new PdfPCell(new Phrase("Name"));
  128.         cell.setBorder(Rectangle.NO_BORDER);
  129.         innertable.addCell(cell);
  130.         // column 3
  131.         cell = new PdfPCell();
  132.         cell.setBorder(Rectangle.NO_BORDER);
  133.         innertable.addCell(cell);
  134.         // column 4
  135.         cell = new PdfPCell(new Phrase("Signature: "));
  136.         cell.setBorder(Rectangle.NO_BORDER);
  137.         innertable.addCell(cell);
  138.         // spacing
  139.         cell = new PdfPCell();
  140.         cell.setColspan(4);
  141.         cell.setFixedHeight(3);
  142.         cell.setBorder(Rectangle.NO_BORDER);
  143.         innertable.addCell(cell);
  144.         // subsequent rows
  145.         for (int i = 1; i < 4; i++) {
  146.             // column 1
  147.             cell = new PdfPCell(new Phrase(String.format("%s:", i)));
  148.             cell.setBorder(Rectangle.NO_BORDER);
  149.             innertable.addCell(cell);
  150.             // column 2
  151.             cell = new PdfPCell();
  152.             innertable.addCell(cell);
  153.             // column 3
  154.             cell = new PdfPCell();
  155.             cell.setBorder(Rectangle.NO_BORDER);
  156.             innertable.addCell(cell);
  157.             // column 4
  158.             cell = new PdfPCell();
  159.             innertable.addCell(cell);
  160.             // spacing
  161.             cell = new PdfPCell();
  162.             cell.setColspan(4);
  163.             cell.setFixedHeight(3);
  164.             cell.setBorder(Rectangle.NO_BORDER);
  165.             innertable.addCell(cell);
  166.         }
  167.         // second nested table
  168.         cell = new PdfPCell(innertable);
  169.         cell.setCellEvent(roundRectangle);
  170.         cell.setBorder(Rectangle.NO_BORDER);
  171.         cell.setPadding(8);
  172.         outertable.addCell(cell);
  173.         // add the table
  174.         document.add(outertable);
  175.         document.close();
  176.     }
  177. }
NestedTables4.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/36259214
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  13. import com.itextpdf.text.pdf.PdfPCell;
  14. import com.itextpdf.text.pdf.PdfPTable;
  15. import com.itextpdf.text.pdf.PdfPTableEvent;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. /**
  23.  *
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. @WrapToTest
  27. public class NestedTables4 {
  28.    
  29.     public static final String DEST = "results/tables/nested_tables4.pdf";
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new NestedTables4().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document(PageSize.A4.rotate());
  39.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         PdfPTable table = new PdfPTable(4);
  42.         table.setTableEvent(new BorderEvent());
  43.         table.setWidths(new int[]{1, 12, 8, 1});
  44.         table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
  45.         // first row
  46.         PdfPCell cell = new PdfPCell(new Phrase("Main table"));
  47.         cell.setBorder(Rectangle.NO_BORDER);
  48.         cell.setColspan(4);
  49.         table.addCell(cell);
  50.         // second row
  51.         table.addCell("");
  52.         table.addCell("nested table 1");
  53.         table.addCell("nested table 2");
  54.         table.addCell("");
  55.         // third row
  56.         // third row cell 1
  57.         table.addCell("");
  58.         // third row cell 2
  59.         PdfPTable table1 = new PdfPTable(1);
  60.         table1.addCell("cell 1 of nested table 1");
  61.         table1.addCell("cell 2 of nested table 1");
  62.         table1.addCell("cell 2 of nested table 1");
  63.         table.addCell(new PdfPCell(table1));
  64.         // third row cell 3
  65.         PdfPTable table2 = new PdfPTable(2);
  66.         table2.getDefaultCell().setMinimumHeight(10);
  67.         table2.addCell("");
  68.         table2.addCell("");
  69.         cell = new PdfPCell(new Phrase("cell 2 of nested table 2"));
  70.         cell.setColspan(2);
  71.         table2.addCell(cell);
  72.         cell = new PdfPCell(new Phrase("cell 3 of nested table 2"));
  73.         cell.setColspan(2);
  74.         table2.addCell(cell);
  75.         table.addCell(new PdfPCell(table2));
  76.         // third row cell 4
  77.         table.addCell("");
  78.         // fourth row
  79.         cell = new PdfPCell();
  80.         cell.setColspan(4);
  81.         cell.setBorder(Rectangle.NO_BORDER);
  82.         cell.setMinimumHeight(16);
  83.         table.addCell(cell);
  84.         document.add(table);
  85.         document.close();
  86.     }
  87.    
  88.     public class BorderEvent implements PdfPTableEvent {
  89.         public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
  90.             float width[] = widths[0];
  91.             float x1 = width[0];
  92.             float x2 = width[width.length - 1];
  93.             float y1 = heights[0];
  94.             float y2 = heights[heights.length - 1];
  95.             PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
  96.             cb.rectangle(x1, y1, x2 - x1, y2 - y1);
  97.             cb.stroke();
  98.         }
  99.     }
  100. }
NestedTables5.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/36259214
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  *
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class NestedTables5 {
  25.    
  26.     public static final String DEST = "results/tables/nested_tables5.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new NestedTables5().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         // Header part
  39.         PdfPTable table = new PdfPTable(2);
  40.         table.setWidths(new int[]{50, 50});
  41.         // first cell
  42.         PdfPTable table1 = new PdfPTable(1);
  43.         table1.getDefaultCell().setMinimumHeight(30);
  44.         table1.addCell("Address 1");
  45.         table1.addCell("Address 2");
  46.         table1.addCell("Address 3");
  47.         table.addCell(new PdfPCell(table1));
  48.         // second cell
  49.         PdfPTable table2 = new PdfPTable(2);
  50.         table2.addCell("Date");
  51.         table2.addCell("Place");
  52.         PdfPCell cell = new PdfPCell(new Phrase("References"));
  53.         cell.setMinimumHeight(40);
  54.         cell.setColspan(2);
  55.         table2.addCell(cell);
  56.         cell = new PdfPCell(new Phrase("destination"));
  57.         cell.setColspan(2);
  58.         table2.addCell(cell);
  59.         table.addCell(new PdfPCell(table2));
  60.         // second row
  61.         cell = new PdfPCell();
  62.         cell.setColspan(2);
  63.         cell.setMinimumHeight(16);
  64.         table.addCell(cell);
  65.         document.add(table);
  66.         // Body part
  67.         table = new PdfPTable(6);
  68.         table.setWidths(new int[]{ 1, 2, 6, 1, 2, 2 });
  69.         table.addCell("sl no");
  70.         table.addCell("qty");
  71.         table.addCell("Product");
  72.         table.addCell("units");
  73.         table.addCell("rate");
  74.         table.addCell("total");
  75.         table.setHeaderRows(1);
  76.         for (int i = 0; i < 6; ) {
  77.             table.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT);
  78.             table.getDefaultCell().setMinimumHeight(16);
  79.             table.addCell(String.valueOf(++i));
  80.             table.addCell("");
  81.             table.addCell("");
  82.             table.addCell("");
  83.             table.addCell("");
  84.             table.addCell("");
  85.         }
  86.         table.getDefaultCell().setFixedHeight(3);
  87.         table.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
  88.         table.addCell("");
  89.         table.addCell("");
  90.         table.addCell("");
  91.         table.addCell("");
  92.         table.addCell("");
  93.         table.addCell("");
  94.         document.add(table);
  95.         document.close();
  96.     }
  97. }
NestedTables6.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/37548146
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Phrase;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfPCell;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  *
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class NestedTables6 {
  25.    
  26.     public static final String DEST = "results/tables/nested_tables6.pdf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new NestedTables6().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document(new Rectangle(1200, 800));
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         // Header part
  39.         PdfPTable mainTable = new PdfPTable(1);
  40.         mainTable.getDefaultCell().setPadding(0);
  41.         mainTable.setTotalWidth(1000);
  42.         mainTable.setLockedWidth(true);
  43.         PdfPTable subTable1 = new PdfPTable(5);
  44.         subTable1.setTotalWidth(new float[]{200, 200, 200, 100, 300});
  45.         subTable1.setLockedWidth(true);
  46.         subTable1.addCell("test 1");
  47.         subTable1.addCell("test 2");
  48.         subTable1.addCell("test 3");
  49.         subTable1.addCell("test 4");
  50.         subTable1.addCell("test 5");
  51.         mainTable.addCell(subTable1);
  52.         PdfPTable subTable2 = new PdfPTable(5);
  53.         subTable2.setTotalWidth(new float[]{200, 100, 200, 200, 300});
  54.         subTable2.setLockedWidth(true);
  55.         subTable2.addCell("test 1");
  56.         subTable2.addCell("test 2");
  57.         subTable2.addCell("test 3");
  58.         subTable2.addCell("test 4");
  59.         subTable2.addCell("test 5");
  60.         PdfPCell cell2 = new PdfPCell(subTable2);
  61.         mainTable.addCell(cell2);
  62.         PdfPTable subTable3 = new PdfPTable(5);
  63.         subTable3.setTotalWidth(new float[]{200, 200, 100, 200, 300});
  64.         subTable3.setLockedWidth(true);
  65.         subTable3.addCell("test 1");
  66.         subTable3.addCell("test 2");
  67.         subTable3.addCell("test 3");
  68.         subTable3.addCell("test 4");
  69.         subTable3.addCell("test 5");
  70.         PdfPCell cell3 = new PdfPCell();
  71.         cell3.setPadding(0);
  72.         cell3.addElement(subTable3);
  73.         mainTable.addCell(cell3);
  74.         document.add(mainTable);
  75.         document.close();
  76.     }
  77. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now