Render data as table

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

24th October 2015
admin-marketing

Switch code for this example

ArrayToTable.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/24404686/i-need-to-create-a-table-and-assign-the-values-into-the-table-in-pdf-using-javaf
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  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 java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.List;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ArrayToTable {
  22.  
  23.     public static final String DEST = "results/tables/array_to_table.pdf";
  24.    
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new ArrayToTable().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.         PdfPTable table = new PdfPTable(8);
  36.         table.setWidthPercentage(100);
  37.         List dataset = getData();
  38.         for (List record : dataset) {
  39.             for (String field : record) {
  40.                 table.addCell(field);
  41.             }
  42.         }
  43.         document.add(table);
  44.         document.close();
  45.     }
  46.    
  47.     public List getData() {
  48.         List data = new ArrayList();
  49.         String[] tableTitleList = {" Title", " (Re)set", " Obs", " Mean", " Std.Dev", " Min", " Max", "Unit"};
  50.         data.add(Arrays.asList(tableTitleList));
  51.         for (int i = 0; i < 10; ) {
  52.             List dataLine = new ArrayList();
  53.             i++;
  54.             for (int j = 0; j < tableTitleList.length; j++) {
  55.                 dataLine.add(tableTitleList[j] + " " + i);
  56.             }
  57.             data.add(dataLine);
  58.         }
  59.         return data;
  60.     }
  61. }
UnitedStates.java
  1. /**
  2.  * This example was 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.Font;
  9. import com.itextpdf.text.Font.FontFamily;
  10. import com.itextpdf.text.PageSize;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.PdfPTable;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.FileReader;
  19. import java.io.IOException;
  20. import java.util.StringTokenizer;
  21.  
  22. import sandbox.WrapToTest;
  23.  
  24. @WrapToTest
  25. public class UnitedStates {
  26.  
  27.     public static final String DEST = "results/tables/united_states.pdf";
  28.     public static final String DATA = "resources/data/united_states.csv";
  29.     public static final Font FONT = new Font();
  30.     public static final Font BOLD = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
  31.  
  32.     public static void main(String[] args) throws IOException, DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new UnitedStates().createPdf(DEST);
  36.     }
  37.    
  38.     public void createPdf(String dest) throws IOException, DocumentException {
  39.         Document document = new Document(PageSize.A4.rotate());
  40.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  41.         document.open();
  42.         PdfPTable table = new PdfPTable(9);
  43.         table.setWidthPercentage(100);
  44.         table.setWidths(new int[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
  45.         BufferedReader br = new BufferedReader(new FileReader(DATA));
  46.         String line = br.readLine();
  47.         process(table, line, BOLD);
  48.         table.setHeaderRows(1);
  49.         while ((line = br.readLine()) != null) {
  50.             process(table, line, FONT);
  51.         }
  52.         br.close();
  53.         document.add(table);
  54.         document.close();
  55.     }
  56.    
  57.     public void process(PdfPTable table, String line, Font font) {
  58.         StringTokenizer tokenizer = new StringTokenizer(line, ";");
  59.         while (tokenizer.hasMoreTokens()) {
  60.             table.addCell(new Phrase(tokenizer.nextToken(), font));
  61.         }
  62.     }
  63. }
RowColumnOrder.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/37526223
  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. @WrapToTest
  18. public class RowColumnOrder {
  19.    
  20.     public static final String DEST = "results/tables/row_column_order.pdf";
  21.    
  22.     public static void main(String[] args) throws IOException, DocumentException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new RowColumnOrder().createPdf(DEST);
  26.     }
  27.  
  28.     public void createPdf(String dest) throws IOException, DocumentException {
  29.         Document document = new Document();
  30.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  31.         document.open();
  32.         document.add(new Paragraph("By design tables are filled row by row:"));
  33.         PdfPTable table = new PdfPTable(5);
  34.         table.setSpacingBefore(10);
  35.         table.setSpacingAfter(10);
  36.         for (int i = 1; i
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