Tables to show keys and values

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


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