Tables to show keys and values

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

5th September 2016
iText PDF

Switch code for this example

KeyValueTable.java
  1. /*
  2.  * This question was written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/39190026
  4.  */
  5. package com.itextpdf.sandbox.tables;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  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.Table;
  15. import com.itextpdf.test.annotations.WrapToTest;
  16. import java.io.File;
  17. import java.io.IOException;
  18.  
  19. /**
  20.  * @author bruno
  21.  */
  22. @WrapToTest
  23. public class KeyValueTable {
  24.  
  25.     public static final String DEST = "results/tables/key_value_table.pdf";
  26.  
  27.     public final PdfFont regular;
  28.     public final PdfFont bold;
  29.  
  30.     public static void main(String[] args) throws IOException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new KeyValueTable().createPdf(DEST);
  34.     }
  35.  
  36.     public KeyValueTable() throws IOException {
  37.         this.regular = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  38.         this.bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
  39.     }
  40.  
  41.     public void createPdf(String dest) throws IOException {
  42.         UserObject rohit = new UserObject();
  43.         rohit.setName("Rohit");
  44.         rohit.setId("6633429");
  45.         rohit.setReputation(1);
  46.         rohit.setJobtitle("Copy/paste artist");
  47.  
  48.         UserObject bruno = new UserObject();
  49.         bruno.setName("Bruno Lowagie");
  50.         bruno.setId("1622493");
  51.         bruno.setReputation(42690);
  52.         bruno.setJobtitle("Java Rockstar");
  53.  
  54.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  55.         Document document = new Document(pdf);
  56.         document.add(createTable(rohit));
  57.         document.add(createTable(bruno));
  58.         document.close();
  59.     }
  60.  
  61.     public Table createTable(UserObject user) {
  62.         Table table = new Table(2);
  63.         table.setWidthPercent(30).setMarginBottom(10);
  64.         table.addHeaderCell(new Cell().setFont(bold).add("Key"));
  65.         table.addHeaderCell(new Cell().setFont(bold).add("Value"));
  66.         table.addCell(new Cell().setFont(bold).add("Name"));
  67.         table.addCell(new Cell().setFont(regular).add(user.getName()));
  68.         table.addCell(new Cell().setFont(bold).add("Id"));
  69.         table.addCell(new Cell().setFont(regular).add(user.getId()));
  70.         table.addCell(new Cell().setFont(bold).add("Reputation"));
  71.         table.addCell(new Cell().setFont(regular).add(String.valueOf(user.getReputation())));
  72.         table.addCell(new Cell().setFont(bold).add("Job title"));
  73.         table.addCell(new Cell().setFont(regular).add(user.getJobtitle()));
  74.         return table;
  75.     }
  76.  
  77.     class UserObject {
  78.  
  79.         protected String name;
  80.         protected String id;
  81.         protected int reputation;
  82.         protected String jobtitle;
  83.  
  84.         public String getName() {
  85.             return name;
  86.         }
  87.  
  88.         public void setName(String name) {
  89.             this.name = name;
  90.         }
  91.  
  92.         public String getId() {
  93.             return id;
  94.         }
  95.  
  96.         public void setId(String id) {
  97.             this.id = id;
  98.         }
  99.  
  100.         public int getReputation() {
  101.             return reputation;
  102.         }
  103.  
  104.         public void setReputation(int reputation) {
  105.             this.reputation = reputation;
  106.         }
  107.  
  108.         public String getJobtitle() {
  109.             return jobtitle;
  110.         }
  111.  
  112.         public void setJobtitle(String jobtitle) {
  113.             this.jobtitle = jobtitle;
  114.         }
  115.     }
  116. }
KeyValueTable2.java
  1. /*
  2.  * This question was written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/39190026
  4.  */
  5. package com.itextpdf.sandbox.tables;
  6.  
  7. import com.itextpdf.io.font.FontConstants;
  8. import com.itextpdf.kernel.font.PdfFont;
  9. import com.itextpdf.kernel.font.PdfFontFactory;
  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.border.Border;
  14. import com.itextpdf.layout.element.Cell;
  15. import com.itextpdf.layout.element.Table;
  16. import com.itextpdf.test.annotations.WrapToTest;
  17. import java.io.File;
  18. import java.io.IOException;
  19.  
  20. /**
  21.  * @author bruno
  22.  */
  23. @WrapToTest
  24. public class KeyValueTable2 {
  25.  
  26.     public static final String DEST = "results/tables/key_value_table2.pdf";
  27.  
  28.     public final PdfFont regular;
  29.     public final PdfFont bold;
  30.  
  31.     public static void main(String[] args) throws IOException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new KeyValueTable2().createPdf(DEST);
  35.     }
  36.  
  37.     public KeyValueTable2() throws IOException {
  38.         this.regular = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  39.         this.bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
  40.     }
  41.  
  42.     public void createPdf(String dest) throws IOException {
  43.         UserObject rohit = new UserObject();
  44.         rohit.setName("Rohit");
  45.         rohit.setId("6633429");
  46.         rohit.setReputation(1);
  47.         rohit.setJobtitle("Copy/paste artist");
  48.  
  49.         UserObject bruno = new UserObject();
  50.         bruno.setName("Bruno Lowagie");
  51.         bruno.setId("1622493");
  52.         bruno.setReputation(42690);
  53.         bruno.setJobtitle("Java Rockstar");
  54.  
  55.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  56.         Document document = new Document(pdf);
  57.         document.add(createTable(rohit, bruno));
  58.         document.close();
  59.     }
  60.  
  61.     public Table createTable(UserObject user1, UserObject user2) {
  62.         if (user1 == null) user1 = new UserObject();
  63.         if (user2 == null) user2 = new UserObject();
  64.         Table table = new Table(3);
  65.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add("Name:"));
  66.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user1.getName()));
  67.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user2.getName()));
  68.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add("Id:"));
  69.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user1.getId()));
  70.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user2.getId()));
  71.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add("Reputation:"));
  72.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(String.valueOf(user1.getReputation())));
  73.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(String.valueOf(user2.getReputation())));
  74.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(bold).add("Job title:"));
  75.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user1.getJobtitle()));
  76.         table.addCell(new Cell().setBorder(Border.NO_BORDER).setFont(regular).add(user2.getJobtitle()));
  77.         return table;
  78.     }
  79.  
  80.     class UserObject {
  81.  
  82.         protected String name = "";
  83.         protected String id = "";
  84.         protected int reputation = 0;
  85.         protected String jobtitle = "";
  86.  
  87.         public String getName() {
  88.             return name;
  89.         }
  90.  
  91.         public void setName(String name) {
  92.             this.name = name;
  93.         }
  94.  
  95.         public String getId() {
  96.             return id;
  97.         }
  98.  
  99.         public void setId(String id) {
  100.             this.id = id;
  101.         }
  102.  
  103.         public int getReputation() {
  104.             return reputation;
  105.         }
  106.  
  107.         public void setReputation(int reputation) {
  108.             this.reputation = reputation;
  109.         }
  110.  
  111.         public String getJobtitle() {
  112.             return jobtitle;
  113.         }
  114.  
  115.         public void setJobtitle(String jobtitle) {
  116.             this.jobtitle = jobtitle;
  117.         }
  118.     }
  119. }
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