Large tables

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

8th November 2015
admin-marketing

Switch code for this example

Example that shows how you can add a table little by little in order to keep the memory footprint low.



IncompleteTable.java
  1. /**
  2.  * Example written by Bruno Lowagie
  3.  */
  4. package sandbox.tables;
  5.  
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.PageSize;
  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 IncompleteTable {
  19.     public static final String DEST = "results/tables/incomplete_table.pdf";
  20.  
  21.     public static void main(String[] args) throws IOException,
  22.             DocumentException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new IncompleteTable().createPdf(DEST);
  26.     }
  27.     public void createPdf(String dest) throws IOException, DocumentException {
  28.         Document document = new Document(PageSize.LETTER);
  29.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  30.  
  31.         document.open();
  32.         PdfPTable table = new PdfPTable(5);
  33.         table.setHeaderRows(1);
  34.         table.setSplitRows(false);
  35.         table.setComplete(false);
  36.  
  37.         for (int i = 0; i < 5; i++) {table.addCell("Header " + i);}
  38.  
  39.         for (int i = 0; i < 500; i++) {
  40.             if (i%5 == 0) {
  41.                 document.add(table);
  42.             }
  43.             table.addCell("Test " + i);
  44.         }
  45.  
  46.         table.setComplete(true);
  47.         document.add(table);
  48.         document.close();
  49.     }
  50.  
  51. }
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