1
|
package vldc.aswi.utils;
|
2
|
|
3
|
import com.lowagie.text.*;
|
4
|
import com.lowagie.text.Font;
|
5
|
import com.lowagie.text.alignment.HorizontalAlignment;
|
6
|
import com.lowagie.text.alignment.VerticalAlignment;
|
7
|
import com.lowagie.text.pdf.PdfWriter;
|
8
|
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
|
9
|
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.FileOutputStream;
|
13
|
import java.util.List;
|
14
|
|
15
|
/**
|
16
|
* Class used for exporting data to PDF files.
|
17
|
*/
|
18
|
public class ExporterPDF {
|
19
|
|
20
|
/**
|
21
|
* Exports data to .pdf file
|
22
|
* @param tmpFile - Temporary file.
|
23
|
* @param contingencyTableRows - Contingency table rows.
|
24
|
*/
|
25
|
public static void export(File tmpFile, List<ContingencyTableRow> contingencyTableRows) {
|
26
|
|
27
|
try {
|
28
|
if (contingencyTableRows.size() == 0) {
|
29
|
|
30
|
return;
|
31
|
}
|
32
|
|
33
|
// Create new document.
|
34
|
Document doc = new Document();
|
35
|
|
36
|
// Get instance of PdfWriter.
|
37
|
PdfWriter.getInstance(doc, new FileOutputStream(tmpFile));
|
38
|
|
39
|
// Create fonts.
|
40
|
Font colRowFont = new Font(Font.HELVETICA, 14, Font.BOLD);
|
41
|
Font dataFont = new Font(Font.HELVETICA, 12);
|
42
|
|
43
|
// Calculate column and row count.
|
44
|
int columnsCount = getColumnCount(contingencyTableRows.get(0));
|
45
|
int rowsCount = contingencyTableRows.size();
|
46
|
|
47
|
// Create table.
|
48
|
Table table = new Table(columnsCount, rowsCount);
|
49
|
|
50
|
for (ContingencyTableRow row : contingencyTableRows) {
|
51
|
|
52
|
if (row.isHeader()) {
|
53
|
// Row is header.
|
54
|
setColRow(table, row.getCells(), colRowFont);
|
55
|
} else {
|
56
|
// Row contains data.
|
57
|
setDataRow(table, row.getCells(), colRowFont, dataFont);
|
58
|
}
|
59
|
}
|
60
|
|
61
|
// Open document and write created table into it.
|
62
|
doc.open();
|
63
|
doc.add(table);
|
64
|
|
65
|
doc.close();
|
66
|
} catch (Exception e) {
|
67
|
|
68
|
System.out.println("PDF export failed.");
|
69
|
}
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Set column header row.
|
74
|
* @param table - Table.
|
75
|
* @param cells - List of contingency table cells.
|
76
|
* @param colRowFont - Column row styles.
|
77
|
* @throws BadElementException If cell is not created successfully.
|
78
|
*/
|
79
|
private static void setColRow(Table table, List<ContingencyTableRowCell> cells, Font colRowFont) throws BadElementException {
|
80
|
|
81
|
for (ContingencyTableRowCell cell : cells) {
|
82
|
|
83
|
// Create and prepare pdf cell.
|
84
|
Cell pdfCell = prepareCell(cell.getValue(), colRowFont, cell.getColSpan());
|
85
|
pdfCell.setHeader(true);
|
86
|
table.addCell(pdfCell);
|
87
|
}
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Set data row.
|
92
|
* @param table - Table.
|
93
|
* @param cells - List of contingency table cells.
|
94
|
* @param colRowFont - Column row styles.
|
95
|
* @param dataFont - Data row styles.
|
96
|
* @throws BadElementException If cell is not created successfully.
|
97
|
*/
|
98
|
private static void setDataRow(Table table, List<ContingencyTableRowCell> cells, Font colRowFont, Font dataFont) throws BadElementException {
|
99
|
|
100
|
boolean headerWrited = false;
|
101
|
|
102
|
for (ContingencyTableRowCell cell : cells) {
|
103
|
|
104
|
// Create pdf cell.
|
105
|
Cell pdfCell;
|
106
|
|
107
|
// First cell is always header, so apply different style.
|
108
|
if (!headerWrited) {
|
109
|
|
110
|
headerWrited = true;
|
111
|
|
112
|
// Prepare pdf cell.
|
113
|
pdfCell = prepareCell(cell.getValue(), colRowFont, cell.getColSpan());
|
114
|
pdfCell.setHeader(true);
|
115
|
|
116
|
} else {
|
117
|
|
118
|
// Prepare pdf cell.
|
119
|
pdfCell = prepareCell(cell.getValue(), dataFont, cell.getColSpan());
|
120
|
}
|
121
|
|
122
|
table.addCell(pdfCell);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* Prepare pdf cell.
|
128
|
* @param value - Cell value.
|
129
|
* @param font - Cell font.
|
130
|
* @param colSpan - Cell colspan.
|
131
|
* @return Prepared pdf cell.
|
132
|
*/
|
133
|
private static Cell prepareCell(String value, Font font, int colSpan) {
|
134
|
|
135
|
Cell cell = new Cell(new Phrase(value, font));
|
136
|
cell.setColspan(colSpan);
|
137
|
cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
138
|
cell.setVerticalAlignment(VerticalAlignment.CENTER);
|
139
|
|
140
|
return cell;
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Calculate column count in contingency table row.
|
145
|
* @param firstRow - First row of contingency table.
|
146
|
* @return Column count in contingency table.
|
147
|
*/
|
148
|
private static int getColumnCount(ContingencyTableRow firstRow) {
|
149
|
|
150
|
int columnCount = 0;
|
151
|
|
152
|
for (ContingencyTableRowCell cell : firstRow.getCells()) {
|
153
|
|
154
|
columnCount += cell.getColSpan();
|
155
|
}
|
156
|
|
157
|
return columnCount;
|
158
|
}
|
159
|
}
|