Projekt

Obecné

Profil

Stáhnout (6.33 KB) Statistiky
| Větev: | Revize:
1
package vldc.aswi.utils;
2

    
3
import be.quodlibet.boxable.BaseTable;
4
import com.lowagie.text.*;
5
import com.lowagie.text.Font;
6
import com.lowagie.text.alignment.HorizontalAlignment;
7
import com.lowagie.text.alignment.VerticalAlignment;
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;
14

    
15
import java.awt.*;
16
import java.io.File;
17
import java.io.FileOutputStream;
18
import java.util.List;
19

    
20
/**
21
 * Class used for exporting data to PDF files.
22
 */
23
public class ExporterPDF {
24

    
25
    /**
26
     * Exports data to .pdf file
27
     * @param tmpFile - Temporary file.
28
     * @param contingencyTableRows - Contingency table rows.
29
     */
30
    public static void export(File tmpFile, List<ContingencyTableRow> contingencyTableRows) {
31

    
32
        try {
33
            if (contingencyTableRows.size() == 0) {
34

    
35
                return;
36
            }
37

    
38
            // Create new document.
39
            Document doc = new Document();
40

    
41
            // Get instance of PdfWriter.
42
            PdfWriter.getInstance(doc, new FileOutputStream(tmpFile));
43

    
44
            // Create fonts.
45
            Font colRowFont = new Font(Font.HELVETICA, 14, Font.BOLD);
46
            Font dataFont = new Font(Font.HELVETICA, 12);
47

    
48
            // Calculate column and row count.
49
            int columnsCount = getColumnCount(contingencyTableRows.get(0));
50
            int rowsCount = contingencyTableRows.size();
51

    
52
            // Create table.
53
            Table table = new Table(columnsCount, rowsCount);
54

    
55
            for (ContingencyTableRow row : contingencyTableRows) {
56

    
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
            }
65

    
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
         */
204
    }
205
}
(3-3/5)