-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPdfFileBuilder.java
More file actions
117 lines (103 loc) · 3.26 KB
/
PdfFileBuilder.java
File metadata and controls
117 lines (103 loc) · 3.26 KB
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
package org.vaadin.haijian.filegenerator;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Calendar;
import java.util.Date;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.vaadin.data.Container;
import com.vaadin.ui.Table;
public class PdfFileBuilder extends FileBuilder {
private Document document;
private PdfPTable table;
private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
Font.BOLD);
private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 14,
Font.BOLD);
private boolean withBorder;
public PdfFileBuilder(Container container) {
super(container);
}
public PdfFileBuilder(Table table) {
super(table);
}
@Override
protected void buildHeader() {
if (getHeader() != null) {
Paragraph title = new Paragraph(getHeader(), catFont);
title.add(new Paragraph(" "));
title.setAlignment(Element.ALIGN_CENTER);
try {
document.add(title);
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
@Override
protected void buildColumnHeaderCell(String header) {
PdfPCell cell = new PdfPCell(new Phrase(header, subFont));
if (!withBorder) {
cell.setBorder(Rectangle.NO_BORDER);
}
table.addCell(cell);
}
@Override
protected void buildCell(Object modelValue, Object presentationValue) {
PdfPCell cell;
if(modelValue == null){
cell = new PdfPCell(new Phrase(""));
}else if(modelValue instanceof Calendar){
Calendar calendar = (Calendar) modelValue;
cell = new PdfPCell(new Phrase(formatDate(calendar.getTime())));
}else if(modelValue instanceof Date){
cell = new PdfPCell(new Phrase(formatDate((Date) modelValue)));
}else {
cell = new PdfPCell(new Phrase(presentationValue.toString()));
}
if (!withBorder) {
cell.setBorder(Rectangle.NO_BORDER);
}
table.addCell(cell);
}
@Override
protected String getFileExtension() {
return ".pdf";
}
@Override
protected void writeToFile() {
try {
document.add(table);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
public boolean isWithBorder() {
return withBorder;
}
public void setWithBorder(boolean withBorder) {
this.withBorder = withBorder;
}
@Override
protected void resetContent() {
document = new Document();
table = new PdfPTable(getNumberofColumns());
try {
PdfWriter.getInstance(document, new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
document.open();
}
}