Render data as table

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

31st May 2016
admin-marketing

Switch code for this example

ArrayToTable.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/24404686/i-need-to-create-a-table-and-assign-the-values-into-the-table-in-pdf-using-javaf
  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.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  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. import java.util.ArrayList;
  27. import java.util.Arrays;
  28. import java.util.List;
  29.  
  30. @Category(SampleTest.class)
  31. public class ArrayToTable extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/tables/array_to_table.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new ArrayToTable().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  43.         Document doc = new Document(pdfDoc);
  44.  
  45.         Table table = new Table(8);
  46.         List dataset = getData();
  47.         for (List record : dataset) {
  48.             for (String field : record) {
  49.                 table.addCell(new Cell().add(new Paragraph(field)));
  50.             }
  51.         }
  52.         doc.add(table);
  53.  
  54.         doc.close();
  55.     }
  56.  
  57.     public List getData() {
  58.         List data = new ArrayList();
  59.         String[] tableTitleList = {" Title", " (Re)set", " Obs", " Mean", " Std.Dev", " Min", " Max", "Unit"};
  60.         data.add(Arrays.asList(tableTitleList));
  61.         for (int i = 0; i < 10; ) {
  62.             List dataLine = new ArrayList();
  63.             i++;
  64.             for (int j = 0; j < tableTitleList.length; j++) {
  65.                 dataLine.add(tableTitleList[j] + " " + i);
  66.             }
  67.             data.add(dataLine);
  68.         }
  69.         return data;
  70.     }
  71. }
UnitedStates.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.io.font.FontConstants;
  11. import com.itextpdf.kernel.font.PdfFont;
  12. import com.itextpdf.kernel.font.PdfFontFactory;
  13. import com.itextpdf.kernel.geom.PageSize;
  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.Cell;
  18. import com.itextpdf.layout.element.Paragraph;
  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.BufferedReader;
  26. import java.io.File;
  27. import java.io.FileReader;
  28. import java.util.StringTokenizer;
  29.  
  30. @Category(SampleTest.class)
  31. public class UnitedStates extends GenericTest {
  32.     public static final String DATA = "./src/test/resources/data/united_states.csv";
  33.     public static final String DEST = "./target/test/resources/sandbox/tables/united_states.pdf";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new UnitedStates().manipulatePdf(DEST);
  39.     }
  40.  
  41.     public void process(Table table, String line, PdfFont font, boolean isHeader) {
  42.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  43.         while (tokenizer.hasMoreTokens()) {
  44.             if (isHeader) {
  45.                 table.addHeaderCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font)));
  46.             } else {
  47.                 table.addCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font)));
  48.             }
  49.         }
  50.     }
  51.  
  52.     @Override
  53.     protected void manipulatePdf(String dest) throws Exception {
  54.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  55.         Document doc = new Document(pdfDoc, new PageSize(PageSize.A4).rotate());
  56.  
  57.         PdfFont font = PdfFontFactory.createFont(FontConstants.HELVETICA);
  58.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  59.         Table table = new Table(new float[]{4, 1.5f, 3, 4, 3, 3, 3, 3, 1});
  60.         table.setWidthPercent(100);
  61.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  62.         String line = br.readLine();
  63.         process(table, line, bold, true);
  64.         while ((line = br.readLine()) != null) {
  65.             process(table, line, font, false);
  66.         }
  67.         br.close();
  68.         doc.add(table);
  69.  
  70.         doc.close();
  71.     }
  72.  
  73. }
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