Splitting tables

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

31st May 2016
admin-marketing

Switch code for this example

SplitRowAtEndOfPage.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/24616920/last-row-in-itext-table-extending-when-it-shouldnt
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Cell;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class SplitRowAtEndOfPage extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/split_row_at_end_of_page.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new SplitRowAtEndOfPage().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         Table table = new Table(1);
  40.         table.setWidth(550);
  41.         // the number of iterations has been changed in order to provide the same as in itext5 example
  42.         for (int i = 0; i < 6; i++) {
  43.             Cell cell;
  44.             if (i == 5) {
  45.                 cell = new Cell().add("Three\nLines\nHere");
  46.             } else {
  47.                 cell = new Cell().add(Integer.toString(i));
  48.             }
  49.             table.addCell(cell);
  50.         }
  51.  
  52.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  53.         Document doc = new Document(pdfDoc, new PageSize(612, 237));
  54.         doc.add(table);
  55.         doc.close();
  56.     }
  57. }
SplitRowAtSpecificRow.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/24665167/table-keeprowstogether-in-itext-5-5-1-doesnt-seem-to-work-correctly
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.geom.PageSize;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Cell;
  19. import com.itextpdf.layout.element.Table;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class SplitRowAtSpecificRow extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/split_row_at_specific_row.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new SplitRowAtSpecificRow().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         Table table = new Table(1);
  40.         table.setWidth(550);
  41.         // the number of iterations has been changed in order to provide the same as in itext5 example
  42.         for (int i = 0; i < 6; i++) {
  43.             Cell cell;
  44.             if (i == 5) {
  45.                 cell = new Cell().add("Three\nLines\nHere");
  46.             } else {
  47.                 cell = new Cell().add(Integer.toString(i));
  48.             }
  49.             table.addCell(cell);
  50.         }
  51.  
  52.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  53.         Document doc = new Document(pdfDoc, new PageSize(612, 237));
  54.         doc.add(table);
  55.         doc.close();
  56.     }
  57. }
Splitting.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.border.Border;
  18. import com.itextpdf.layout.element.Cell;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.layout.element.Table;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import java.io.File;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. @Category(SampleTest.class)
  29. public class Splitting extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/tables/splitting.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new Splitting().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  41.         Document doc = new Document(pdfDoc);
  42.         Paragraph p = new Paragraph("Test");
  43.         Table table = new Table(2);
  44.         for (int i = 1; i < 6; i++) {
  45.             table.addCell("key " + i);
  46.             table.addCell("value " + i);
  47.         }
  48.         for (int i = 0; i < 27; i++) {
  49.             doc.add(p);
  50.         }
  51.         doc.add(table);
  52.         for (int i = 0; i < 23; i++) {
  53.             doc.add(p);
  54.         }
  55.         table = new Table(2);
  56.         for (int i = 1; i < 6; i++) {
  57.             table.addCell("key " + i);
  58.             table.addCell("value " + i);
  59.         }
  60.  
  61.         Table nesting = new Table(1);
  62.         Cell cell = new Cell().add(table);
  63.         cell.setBorder(Border.NO_BORDER);
  64.         nesting.addCell(cell.setKeepTogether(true));
  65.         doc.add(nesting);
  66.         doc.close();
  67.     }
  68. }
Splitting2.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.layout.Document;
  17. import com.itextpdf.layout.element.Paragraph;
  18. import com.itextpdf.layout.element.Table;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import java.io.File;
  23.  
  24. import org.junit.Ignore;
  25. import org.junit.experimental.categories.Category;
  26.  
  27. @Category(SampleTest.class)
  28. public class Splitting2 extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/tables/splitting2.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new Splitting2().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  40.         Document doc = new Document(pdfDoc);
  41.         Paragraph p = new Paragraph("Test");
  42.         Table table = new Table(2);
  43.         for (int i = 1; i < 6; i++) {
  44.             table.addCell("key " + i);
  45.             table.addCell("value " + i);
  46.         }
  47.         for (int i = 0; i < 27; i++) {
  48.             doc.add(p);
  49.         }
  50.         doc.add(table);
  51.         for (int i = 0; i < 23; i++) {
  52.             doc.add(p);
  53.         }
  54.         table = new Table(2);
  55.         for (int i = 1; i < 6; i++) {
  56.             table.addCell("key " + i);
  57.             table.addCell("value " + i);
  58.         }
  59.         table.setKeepTogether(true);
  60.         doc.add(table);
  61.         doc.close();
  62.     }
  63. }
TableSplitTest.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Ramesh in the context of a question on SO:
  10.  * http://stackoverflow.com/questions/29345454/itext-avoid-row-splitting-in-table
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.geom.PageSize;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
  21. import com.itextpdf.kernel.pdf.canvas.draw.SolidLine;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.border.SolidBorder;
  24. import com.itextpdf.layout.element.Cell;
  25. import com.itextpdf.layout.element.LineSeparator;
  26. import com.itextpdf.layout.element.Paragraph;
  27. import com.itextpdf.layout.element.Table;
  28. import com.itextpdf.layout.property.TextAlignment;
  29. import com.itextpdf.layout.property.VerticalAlignment;
  30. import com.itextpdf.samples.GenericTest;
  31. import com.itextpdf.test.annotations.type.SampleTest;
  32.  
  33. import java.io.File;
  34.  
  35. import org.junit.experimental.categories.Category;
  36.  
  37.  
  38. @Category(SampleTest.class)
  39. public class TableSplitTest extends GenericTest {
  40.     public static final String DEST = "./target/test/resources/sandbox/tables/tables_split_test.pdf";
  41.  
  42.     public static void main(String[] args) throws Exception {
  43.         File file = new File(DEST);
  44.         file.getParentFile().mkdirs();
  45.         new TableSplitTest().manipulatePdf(DEST);
  46.     }
  47.  
  48.     @Override
  49.     protected void manipulatePdf(String dest) throws Exception {
  50.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  51.         Document doc = new Document(pdfDoc, new PageSize(595, 842));
  52.         doc.setMargins(55, 15, 35, 15);
  53.  
  54.         ILineDrawer line = new SolidLine(2);
  55.         line.setColor(Color.LIGHT_GRAY);
  56.         LineSeparator tableEndSeparator = new LineSeparator(line);
  57.  
  58.         tableEndSeparator.setMarginTop(10);
  59.  
  60.         String[] header = new String[]{"Header1", "Header2", "Header3",
  61.                 "Header4", "Header5"};
  62.         String[] content = new String[]{"column 1", "column 2",
  63.                 "some Text in column 3", "Test data ", "column 5"};
  64.  
  65.         Table table = new Table(new float[]{3, 2, 4, 3, 2});
  66.         table.setWidthPercent(98);
  67.  
  68.         for (String columnHeader : header) {
  69.             Cell headerCell = new Cell().add(new Paragraph(columnHeader).setFont(
  70.                     PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD))
  71.                     .setFontSize(10));
  72.             headerCell.setTextAlignment(TextAlignment.CENTER);
  73.             headerCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
  74.             headerCell.setBorder(new SolidBorder(Color.LIGHT_GRAY, 1));
  75.             headerCell.setPadding(8);
  76.             table.addHeaderCell(headerCell);
  77.         }
  78.         for (int i = 0; i < 15; i++) {
  79.             int j = 0;
  80.             for (String text : content) {
  81.                 if (i == 13 && j == 3) {
  82.                     text = "Test data \n Test data \n Test data";
  83.                 }
  84.                 j++;
  85.                 Cell cell = new Cell().add(new Paragraph(text).setFont(
  86.                         PdfFontFactory.createFont(FontConstants.HELVETICA))
  87.                         .setFontSize(10));
  88.                 cell.setBorder(new SolidBorder(Color.LIGHT_GRAY, 1));
  89.                 cell.setPaddingLeft(5);
  90.                 cell.setPaddingTop(5);
  91.                 cell.setPaddingRight(5);
  92.                 cell.setPaddingBottom(5);
  93.                 table.addCell(cell);
  94.             }
  95.         }
  96.         doc.add(table);
  97.         doc.add(tableEndSeparator);
  98.         for (int k = 0; k < 5; k++) {
  99.             Paragraph info = new Paragraph("Some title").setFont(
  100.                     PdfFontFactory.createFont(FontConstants.HELVETICA))
  101.                     .setFontSize(10);
  102.             info.setMarginTop(12f);
  103.             doc.add(info);
  104.             table = new Table(new float[]{3, 2, 4, 3, 2});
  105.             table.setWidthPercent(98);
  106.             table.setMarginTop(15);
  107.  
  108.             for (String columnHeader : header) {
  109.                 Cell headerCell = new Cell().add(new Paragraph(columnHeader).setFont(
  110.                         PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD))
  111.                         .setFontSize(10));
  112.                 headerCell.setTextAlignment(TextAlignment.CENTER);
  113.                 headerCell.setVerticalAlignment(VerticalAlignment.MIDDLE);
  114.                 headerCell.setBorder(new SolidBorder(Color.LIGHT_GRAY, 1));
  115.                 headerCell.setPaddingLeft(8);
  116.                 headerCell.setPaddingTop(8);
  117.                 headerCell.setPaddingRight(8);
  118.                 headerCell.setPaddingBottom(8);
  119.                 table.addHeaderCell(headerCell);
  120.             }
  121.             for (String text : content) {
  122.                 Cell cell = new Cell().add(new Paragraph(text).setFont(
  123.                         PdfFontFactory.createFont(FontConstants.HELVETICA))
  124.                         .setFontSize(10));
  125.                 cell.setBorder(new SolidBorder(Color.LIGHT_GRAY, 1));
  126.                 cell.setPaddingLeft(5);
  127.                 cell.setPaddingTop(5);
  128.                 cell.setPaddingRight(5);
  129.                 cell.setPaddingBottom(5);
  130.                 table.addCell(cell);
  131.             }
  132.             doc.add(table);
  133.             doc.add(tableEndSeparator);
  134.         }
  135.  
  136.         doc.close();
  137.     }
  138. }
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