Large tables

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

31st May 2016
iText PDF

Switch code for this example

IncompleteTable.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. package com.itextpdf.samples.sandbox.tables;
  9.  
  10. import com.itextpdf.kernel.pdf.PdfDocument;
  11. import com.itextpdf.kernel.pdf.PdfWriter;
  12. import com.itextpdf.layout.Document;
  13. import com.itextpdf.layout.element.Cell;
  14. import com.itextpdf.layout.element.Paragraph;
  15. import com.itextpdf.layout.element.Table;
  16. import com.itextpdf.samples.GenericTest;
  17. import com.itextpdf.test.annotations.type.SampleTest;
  18.  
  19. import org.junit.experimental.categories.Category;
  20.  
  21. import java.io.File;
  22.  
  23. @Category(SampleTest.class)
  24. public class IncompleteTable extends GenericTest {
  25.     public static final String DEST = "./target/test/resources/sandbox/tables/incomplete_table.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new IncompleteTable().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  36.         Document doc = new Document(pdfDoc);
  37.  
  38.         Table table = new Table(5, true);
  39.  
  40.         for (int i = 0; i < 5; i++) {
  41.             table.addHeaderCell(new Cell().setKeepTogether(true).add(new Paragraph("Header " + i)));
  42.         }
  43.  
  44.         doc.add(table);
  45.         for (int i = 0; i < 500; i++) {
  46.             if (i % 5 == 0) {
  47.                 table.flush();
  48.             }
  49.             table.addCell(new Cell().setKeepTogether(true).add(new Paragraph("Test " + i).setMargins(0, 0, 0, 0)));
  50.         }
  51.  
  52.         table.complete();
  53.  
  54.         doc.close();
  55.     }
  56. }
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