Skip to content

Commit 042d797

Browse files
committed
TableBuilder.java
TableBuilder.java
1 parent da09dca commit 042d797

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.hmkcode;
2+
3+
import com.itextpdf.text.BaseColor;
4+
import com.itextpdf.text.DocumentException;
5+
import com.itextpdf.text.Font;
6+
import com.itextpdf.text.Phrase;
7+
import com.itextpdf.text.Font.FontFamily;
8+
import com.itextpdf.text.pdf.PdfPCell;
9+
import com.itextpdf.text.pdf.PdfPTable;
10+
11+
public class TableBuilder {
12+
// create table
13+
public static PdfPTable createTable() throws DocumentException {
14+
15+
16+
// create 6 column table
17+
PdfPTable table = new PdfPTable(6);
18+
19+
// set the width of the table to 100% of page
20+
table.setWidthPercentage(100);
21+
22+
// set relative columns width
23+
table.setWidths(new float[]{0.6f, 1.4f, 0.8f,0.8f,1.8f,2.6f});
24+
25+
26+
// ----------------Table Header "Title"----------------
27+
// font
28+
Font font = new Font(FontFamily.HELVETICA, 14, Font.BOLD, BaseColor.WHITE);
29+
// create header cell
30+
PdfPCell cell = new PdfPCell(new Phrase("HMKCODE.com - iText PDFTable Example",font));
31+
// set Column span "1 cell = 6 cells width"
32+
cell.setColspan(6);
33+
// set style
34+
Style.headerCellStyle(cell);
35+
// add to table
36+
table.addCell(cell);
37+
38+
//-----------------Table Cells Label/Value------------------
39+
40+
// 1st Row
41+
table.addCell(createLabelCell("Label 1"));
42+
table.addCell(createValueCell("Value 1"));
43+
table.addCell(createLabelCell("Label 2"));
44+
table.addCell(createValueCell("Value 2"));
45+
table.addCell(createLabelCell("Label 3"));
46+
table.addCell(createValueCell("Value 3"));
47+
48+
// 2nd Row
49+
table.addCell(createLabelCell("Label 4"));
50+
table.addCell(createValueCell("Value 4"));
51+
table.addCell(createLabelCell("Label 5"));
52+
table.addCell(createValueCell("Value 5"));
53+
table.addCell(createLabelCell("Label 6"));
54+
table.addCell(createValueCell("Value 6"));
55+
56+
return table;
57+
}
58+
59+
// create cells
60+
private static PdfPCell createLabelCell(String text){
61+
// font
62+
Font font = new Font(FontFamily.HELVETICA, 8, Font.BOLD, BaseColor.DARK_GRAY);
63+
64+
// create cell
65+
PdfPCell cell = new PdfPCell(new Phrase(text,font));
66+
67+
// set style
68+
Style.labelCellStyle(cell);
69+
return cell;
70+
}
71+
72+
// create cells
73+
private static PdfPCell createValueCell(String text){
74+
// font
75+
Font font = new Font(FontFamily.HELVETICA, 8, Font.NORMAL, BaseColor.BLACK);
76+
77+
// create cell
78+
PdfPCell cell = new PdfPCell(new Phrase(text,font));
79+
80+
// set style
81+
Style.valueCellStyle(cell);
82+
return cell;
83+
}
84+
}

0 commit comments

Comments
 (0)