Splitting tables

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

8th November 2015
admin-marketing

Switch code for this example

Splitting.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfPCell;
  11. import com.itextpdf.text.pdf.PdfPTable;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16.  
  17. /**
  18.  *
  19.  * @author Bruno Lowagie (iText Software)
  20.  */
  21. public class Splitting {
  22.     public static final String DEST = "results/tables/splitting.pdf";
  23.     public static void main(String[] args) throws IOException,
  24.             DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new Splitting().createPdf(DEST);
  28.     }
  29.    
  30.     public void createPdf(String dest) throws IOException, DocumentException {
  31.         Document document = new Document();
  32.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  33.         document.open();
  34.         Paragraph p = new Paragraph("Test");
  35.         PdfPTable table = new PdfPTable(2);
  36.         for (int i = 1; i < 6; i++) {
  37.             table.addCell("key " + i);
  38.             table.addCell("value " + i);
  39.         }
  40.         for (int i = 0; i < 40; i++) {
  41.             document.add(p);
  42.         }
  43.         document.add(table);
  44.         for (int i = 0; i < 38; i++) {
  45.             document.add(p);
  46.         }
  47.         PdfPTable nesting = new PdfPTable(1);
  48.         PdfPCell cell = new PdfPCell(table);
  49.         cell.setBorder(PdfPCell.NO_BORDER);
  50.         nesting.addCell(cell);
  51.         document.add(nesting);
  52.         document.close();
  53.     }
  54. }
Splitting2.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/33286841/stop-itext-table-from-spliting-on-new-page
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Paragraph;
  10. import com.itextpdf.text.pdf.PdfPTable;
  11. import com.itextpdf.text.pdf.PdfWriter;
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;
  16.  
  17. /**
  18.  *
  19.  * @author Bruno Lowagie (iText Software)
  20.  */
  21. @WrapToTest
  22. public class Splitting2 {
  23.     public static final String DEST = "results/tables/splitting2.pdf";
  24.     public static void main(String[] args) throws IOException,
  25.             DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new Splitting2().createPdf(DEST);
  29.     }
  30.    
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         Document document = new Document();
  33.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  34.         document.open();
  35.         Paragraph p = new Paragraph("Test");
  36.         PdfPTable table = new PdfPTable(2);
  37.         for (int i = 1; i < 6; i++) {
  38.             table.addCell("key " + i);
  39.             table.addCell("value " + i);
  40.         }
  41.         for (int i = 0; i < 40; i++) {
  42.             document.add(p);
  43.         }
  44.         document.add(table);
  45.         for (int i = 0; i < 38; i++) {
  46.             document.add(p);
  47.         }
  48.         table.setKeepTogether(true);
  49.         document.add(table);
  50.         document.close();
  51.     }
  52. }
TableSplitTest.java
  1. /**
  2.  * Example written by Ramesh in the context of a question on SO:
  3.  * http://stackoverflow.com/questions/29345454/itext-avoid-row-splitting-in-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.Font;
  13. import com.itextpdf.text.FontFactory;
  14. import com.itextpdf.text.Paragraph;
  15. import com.itextpdf.text.Phrase;
  16. import com.itextpdf.text.pdf.PdfPCell;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19. import com.itextpdf.text.pdf.draw.LineSeparator;
  20.  
  21. import java.io.File;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class TableSplitTest {
  28.     public static final String DEST = "results/tables/split_test.pdf";
  29.      
  30.     public static void main(String[] args) throws IOException, DocumentException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new TableSplitTest().createPdf(DEST);
  34.     }
  35.    
  36.     public void createPdf(String dest) throws DocumentException, IOException {
  37.         Document document = new Document();
  38.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  39.         document.setMargins(15, 15, 55, 35);
  40.         document.open();
  41.         String[] header = new String[] { "Header1", "Header2", "Header3",
  42.             "Header4", "Header5" };
  43.     String[] content = new String[] { "column 1", "column 2",
  44.             "some Text in column 3", "Test data ", "column 5" };
  45.         PdfPTable table = new PdfPTable(header.length);
  46.     table.setHeaderRows(1);
  47.     table.setWidths(new int[] { 3, 2, 4, 3, 2 });
  48.         table.setWidthPercentage(98);
  49.         table.setSpacingBefore(15);
  50.         table.setSplitLate(false);
  51.     for (String columnHeader : header) {
  52.             PdfPCell headerCell = new PdfPCell();
  53.             headerCell.addElement(new Phrase(columnHeader, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD)));
  54.             headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  55.             headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  56.             headerCell.setBorderColor(BaseColor.LIGHT_GRAY);
  57.             headerCell.setPadding(8);
  58.             table.addCell(headerCell);
  59.     }
  60.         for (int i = 0; i < 15; i++) {
  61.             int j = 0;
  62.             for (String text : content) {
  63.                 if (i == 13 && j == 3) {
  64.                     text = "Test data \n Test data \n Test data";
  65.                 }
  66.                 j++;
  67.                 PdfPCell cell = new PdfPCell();
  68.                 cell.addElement(new Phrase(text, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL)));
  69.                 cell.setBorderColor(BaseColor.LIGHT_GRAY);
  70.                 cell.setPadding(5);
  71.                 table.addCell(cell);
  72.             }
  73.         }
  74.         document.add(table);
  75.         document.add(new Phrase("\n"));
  76.         LineSeparator separator = new LineSeparator();
  77.         separator.setPercentage(98);
  78.         separator.setLineColor(BaseColor.LIGHT_GRAY);
  79.         Chunk linebreak = new Chunk(separator);
  80.         document.add(linebreak);
  81.         for (int k = 0; k < 5; k++) {
  82.             Paragraph info = new Paragraph("Some title", FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL));
  83.             info.setSpacingBefore(12f);
  84.             document.add(info);
  85.             table = new PdfPTable(header.length);
  86.             table.setHeaderRows(1);
  87.             table.setWidths(new int[] { 3, 2, 4, 3, 2 });
  88.             table.setWidthPercentage(98);
  89.             table.setSpacingBefore(15);
  90.             table.setSplitLate(false);
  91.             for (String columnHeader : header) {
  92.                 PdfPCell headerCell = new PdfPCell();
  93.                 headerCell.addElement(new Phrase(columnHeader, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD)));
  94.                 headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
  95.                 headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
  96.                 headerCell.setBorderColor(BaseColor.LIGHT_GRAY);
  97.                 headerCell.setPadding(8);
  98.                 table.addCell(headerCell);
  99.             }
  100.             for (String text : content) {
  101.                 PdfPCell cell = new PdfPCell();
  102.                 cell.addElement(new Phrase(text, FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL)));
  103.                 cell.setBorderColor(BaseColor.LIGHT_GRAY);
  104.                 cell.setPadding(5);
  105.                 table.addCell(cell);
  106.             }
  107.             document.add(table);
  108.             separator = new LineSeparator();
  109.             separator.setPercentage(98);
  110.             separator.setLineColor(BaseColor.LIGHT_GRAY);
  111.             linebreak = new Chunk(separator);
  112.             document.add(linebreak);
  113.         }
  114.         document.close();
  115.     }
  116.    
  117. }
SplitRowAtSpecificRow.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24665167/table-keeprowstogether-in-itext-5-5-1-doesnt-seem-to-work-correctly
  4.  */
  5.  
  6. package sandbox.tables;
  7.  
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class SplitRowAtSpecificRow {
  23.     public static final String DEST = "results/tables/split_at_row.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new SplitRowAtSpecificRow().createPdf(DEST);
  29.     }
  30.    
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         PdfPTable table = new PdfPTable(1);
  33.         table.setTotalWidth(550);
  34.         table.setLockedWidth(true);
  35.         for (int i = 0; i < 10; i++) {
  36.             PdfPCell cell;
  37.             if (i == 9) {
  38.                 cell = new PdfPCell(new Phrase("Two\nLines"));
  39.             }
  40.             else {
  41.                 cell = new PdfPCell(new Phrase(Integer.toString(i)));
  42.             }
  43.             table.addCell(cell);
  44.         }
  45.         Document document = new Document(new Rectangle(612, 242));
  46.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  47.         document.open();
  48.         table.setSplitLate(false);
  49.         table.setBreakPoints(8);
  50.         document.add(table);
  51.         document.close();
  52.     }
  53. }
SplitRowAtEndOfPage.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24616920/last-row-in-itext-table-extending-when-it-shouldnt
  4.  */
  5.  
  6. package sandbox.tables;
  7.  
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class SplitRowAtEndOfPage {
  23.     public static final String DEST = "results/tables/split_row_at_end_of_page.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new SplitRowAtEndOfPage().createPdf(DEST);
  29.     }
  30.    
  31.     public void createPdf(String dest) throws IOException, DocumentException {
  32.         PdfPTable table = new PdfPTable(1);
  33.         table.setTotalWidth(550);
  34.         table.setLockedWidth(true);
  35.         for (int i = 0; i < 10; i++) {
  36.             PdfPCell cell;
  37.             if (i == 9) {
  38.                 cell = new PdfPCell(new Phrase("Two\nLines"));
  39.             }
  40.             else {
  41.                 cell = new PdfPCell(new Phrase(Integer.toString(i)));
  42.             }
  43.             table.addCell(cell);
  44.         }
  45.         Document document = new Document(new Rectangle(612, 242));
  46.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  47.         document.open();
  48.         table.setSplitLate(false);
  49.         document.add(table);
  50.         document.close();
  51.     }
  52. }
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