Revize d90fc9de
Přidáno uživatelem Vojtěch Danišík před téměř 4 roky(ů)
src/main/java/vldc/aswi/utils/ExporterPDF.java | ||
---|---|---|
1 | 1 |
package vldc.aswi.utils; |
2 | 2 |
|
3 |
import com.lowagie.text.Cell;
|
|
4 |
import com.lowagie.text.Document;
|
|
3 |
import be.quodlibet.boxable.BaseTable;
|
|
4 |
import com.lowagie.text.*;
|
|
5 | 5 |
import com.lowagie.text.Font; |
6 |
import com.lowagie.text.Phrase; |
|
6 |
import com.lowagie.text.alignment.HorizontalAlignment; |
|
7 |
import com.lowagie.text.alignment.VerticalAlignment; |
|
7 | 8 |
import com.lowagie.text.pdf.PdfWriter; |
9 |
import com.sun.media.jfxmedia.logging.Logger; |
|
10 |
import org.apache.pdfbox.pdmodel.PDPage; |
|
11 |
import org.apache.pdfbox.pdmodel.font.PDType1Font; |
|
12 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow; |
|
13 |
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell; |
|
8 | 14 |
|
9 |
import java.io.FileNotFoundException; |
|
15 |
import java.awt.*; |
|
16 |
import java.io.File; |
|
10 | 17 |
import java.io.FileOutputStream; |
18 |
import java.util.List; |
|
11 | 19 |
|
12 | 20 |
/** |
13 |
* Class used for exporting data to PDF files |
|
21 |
* Class used for exporting data to PDF files.
|
|
14 | 22 |
*/ |
15 | 23 |
public class ExporterPDF { |
16 | 24 |
|
17 | 25 |
/** |
18 |
* !!!DUMMY!!! |
|
19 | 26 |
* Exports data to .pdf file |
20 |
* @param tableName name of the table |
|
27 |
* @param tmpFile - Temporary file. |
|
28 |
* @param contingencyTableRows - Contingency table rows. |
|
21 | 29 |
*/ |
22 |
public static void export(String tableName) throws FileNotFoundException { |
|
23 |
Document doc = new Document(); |
|
30 |
public static void export(File tmpFile, List<ContingencyTableRow> contingencyTableRows) { |
|
24 | 31 |
|
25 |
PdfWriter.getInstance(doc, new FileOutputStream("out.pdf")); |
|
32 |
try { |
|
33 |
if (contingencyTableRows.size() == 0) { |
|
26 | 34 |
|
27 |
Font font = new Font(com.lowagie.text.Font.COURIER, 14);
|
|
28 |
com.lowagie.text.Table table = new com.lowagie.text.Table(3);
|
|
35 |
return;
|
|
36 |
}
|
|
29 | 37 |
|
30 |
Cell cell = new Cell(new Phrase(tableName, font)); |
|
31 |
cell.setColspan(2); |
|
32 |
cell.setHeader(true); |
|
38 |
// Create new document. |
|
39 |
Document doc = new Document(); |
|
33 | 40 |
|
34 |
Cell cell2 = new Cell(new Phrase("SUM", font)); |
|
35 |
cell2.setColspan(1); |
|
36 |
cell2.setHeader(true); |
|
41 |
// Get instance of PdfWriter. |
|
42 |
PdfWriter.getInstance(doc, new FileOutputStream(tmpFile)); |
|
37 | 43 |
|
38 |
table.addCell(cell); |
|
39 |
table.addCell(cell2); |
|
44 |
// Create fonts. |
|
45 |
Font colRowFont = new Font(Font.HELVETICA, 14, Font.BOLD); |
|
46 |
Font dataFont = new Font(Font.HELVETICA, 12); |
|
40 | 47 |
|
41 |
table.addCell(new Phrase("Val1", font));
|
|
42 |
table.addCell(new Phrase("Val2", font));
|
|
43 |
table.addCell(new Phrase("", font));
|
|
48 |
// Calculate column and row count.
|
|
49 |
int columnsCount = getColumnCount(contingencyTableRows.get(0));
|
|
50 |
int rowsCount = contingencyTableRows.size();
|
|
44 | 51 |
|
45 |
table.addCell("1"); |
|
46 |
table.addCell("2"); |
|
47 |
table.addCell("3"); |
|
52 |
// Create table. |
|
53 |
Table table = new Table(columnsCount, rowsCount); |
|
48 | 54 |
|
49 |
table.addCell("4"); |
|
50 |
table.addCell("5"); |
|
51 |
table.addCell("9"); |
|
55 |
for (ContingencyTableRow row : contingencyTableRows) { |
|
52 | 56 |
|
53 |
doc.open(); |
|
54 |
doc.add(table); |
|
57 |
if (row.isHeader()) { |
|
58 |
// Row is header. |
|
59 |
setColRow(table, row.getCells(), colRowFont); |
|
60 |
} else { |
|
61 |
// Row contains data. |
|
62 |
setDataRow(table, row.getCells(), colRowFont, dataFont); |
|
63 |
} |
|
64 |
} |
|
55 | 65 |
|
56 |
doc.close(); |
|
66 |
// Open document and write created table into it. |
|
67 |
doc.open(); |
|
68 |
doc.add(table); |
|
69 |
|
|
70 |
doc.close(); |
|
71 |
} catch (Exception e) { |
|
72 |
|
|
73 |
Logger.logMsg(Logger.WARNING, "Error while exporting contingency table to PDF."); |
|
74 |
Logger.logMsg(Logger.WARNING, e.getStackTrace().toString()); |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Set column header row. |
|
80 |
* @param table - Table. |
|
81 |
* @param cells - List of contingency table cells. |
|
82 |
* @param colRowFont - Column row styles. |
|
83 |
* @throws BadElementException If cell is not created successfully. |
|
84 |
*/ |
|
85 |
private static void setColRow(Table table, List<ContingencyTableRowCell> cells, Font colRowFont) throws BadElementException { |
|
86 |
|
|
87 |
for (ContingencyTableRowCell cell : cells) { |
|
88 |
|
|
89 |
// Create and prepare pdf cell. |
|
90 |
Cell pdfCell = prepareCell(cell.getValue(), colRowFont, cell.getColSpan()); |
|
91 |
pdfCell.setHeader(true); |
|
92 |
table.addCell(pdfCell); |
|
93 |
} |
|
94 |
} |
|
95 |
|
|
96 |
/** |
|
97 |
* Set data row. |
|
98 |
* @param table - Table. |
|
99 |
* @param cells - List of contingency table cells. |
|
100 |
* @param colRowFont - Column row styles. |
|
101 |
* @param dataFont - Data row styles. |
|
102 |
* @throws BadElementException If cell is not created successfully. |
|
103 |
*/ |
|
104 |
private static void setDataRow(Table table, List<ContingencyTableRowCell> cells, Font colRowFont, Font dataFont) throws BadElementException { |
|
105 |
|
|
106 |
boolean headerWrited = false; |
|
107 |
|
|
108 |
for (ContingencyTableRowCell cell : cells) { |
|
109 |
|
|
110 |
// Create pdf cell. |
|
111 |
Cell pdfCell; |
|
112 |
|
|
113 |
// First cell is always header, so apply different style. |
|
114 |
if (!headerWrited) { |
|
115 |
|
|
116 |
headerWrited = true; |
|
117 |
|
|
118 |
// Prepare pdf cell. |
|
119 |
pdfCell = prepareCell(cell.getValue(), colRowFont, cell.getColSpan()); |
|
120 |
pdfCell.setHeader(true); |
|
121 |
|
|
122 |
} else { |
|
123 |
|
|
124 |
// Prepare pdf cell. |
|
125 |
pdfCell = prepareCell(cell.getValue(), dataFont, cell.getColSpan()); |
|
126 |
} |
|
127 |
|
|
128 |
table.addCell(pdfCell); |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* Prepare pdf cell. |
|
134 |
* @param value - Cell value. |
|
135 |
* @param font - Cell font. |
|
136 |
* @param colSpan - Cell colspan. |
|
137 |
* @return Prepared pdf cell. |
|
138 |
*/ |
|
139 |
private static Cell prepareCell(String value, Font font, int colSpan) { |
|
140 |
|
|
141 |
Cell cell = new Cell(new Phrase(value, font)); |
|
142 |
cell.setColspan(colSpan); |
|
143 |
cell.setHorizontalAlignment(HorizontalAlignment.CENTER); |
|
144 |
cell.setVerticalAlignment(VerticalAlignment.CENTER); |
|
145 |
|
|
146 |
return cell; |
|
147 |
} |
|
148 |
|
|
149 |
/** |
|
150 |
* Calculate column count in contingency table row. |
|
151 |
* @param firstRow - First row of contingency table. |
|
152 |
* @return Column count in contingency table. |
|
153 |
*/ |
|
154 |
private static int getColumnCount(ContingencyTableRow firstRow) { |
|
155 |
|
|
156 |
int columnCount = 0; |
|
157 |
|
|
158 |
for (ContingencyTableRowCell cell : firstRow.getCells()) { |
|
159 |
|
|
160 |
columnCount += cell.getColSpan(); |
|
161 |
} |
|
162 |
|
|
163 |
return columnCount; |
|
164 |
} |
|
165 |
|
|
166 |
|
|
167 |
public static void exportt(File tmpFile, List<ContingencyTableRow> contingencyTableRows) { |
|
168 |
/* |
|
169 |
//Dummy Table |
|
170 |
float margin = 50; |
|
171 |
|
|
172 |
// starting y position is whole page height subtracted by top and bottom margin |
|
173 |
float yStartNewPage = myPage.getMediaBox().getHeight() - (2 * margin); |
|
174 |
|
|
175 |
// we want table across whole page width (subtracted by left and right margin ofcourse) |
|
176 |
float tableWidth = myPage.getMediaBox().getWidth() - (2 * margin); |
|
177 |
|
|
178 |
boolean drawContent = true; |
|
179 |
float yStart = yStartNewPage; |
|
180 |
float bottomMargin = 70; |
|
181 |
// y position is your coordinate of top left corner of the table |
|
182 |
float yPosition = 550; |
|
183 |
|
|
184 |
BaseTable table = new BaseTable(yPosition, yStartNewPage, bottomMargin, tableWidth, margin, mainDocument, myPage, true, drawContent); |
|
185 |
|
|
186 |
|
|
187 |
Row<PDPage> headerRow = table.createRow(15f); |
|
188 |
Cell<PDPage> cell = headerRow.createCell(100, "Header"); |
|
189 |
table.addHeaderRow(headerRow); |
|
190 |
|
|
191 |
|
|
192 |
Row<PDPage> row = table.createRow(12); |
|
193 |
cell = row.createCell(30, "Data 1"); |
|
194 |
cell = row.createCell(70, "Some value"); |
|
195 |
|
|
196 |
table.draw(); |
|
197 |
|
|
198 |
|
|
199 |
contentStream.close(); |
|
200 |
mainDocument.addPage(myPage); |
|
201 |
mainDocument.save("testfile.pdf"); |
|
202 |
mainDocument.close(); |
|
203 |
*/ |
|
57 | 204 |
} |
58 | 205 |
} |
Také k dispozici: Unified diff
re #8133 #8132 Export contingency table to XLSX + PDF (not fully complete).