Projekt

Obecné

Profil

Stáhnout (6.12 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 org.apache.pdfbox.pdmodel.PDPage;
10
import org.apache.pdfbox.pdmodel.font.PDType1Font;
11
import vldc.aswi.model.table.contingencyTable.ContingencyTableRow;
12
import vldc.aswi.model.table.contingencyTable.ContingencyTableRowCell;
13

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

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

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

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

    
34
                return;
35
            }
36

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

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

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

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

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

    
54
            for (ContingencyTableRow row : contingencyTableRows) {
55

    
56
                if (row.isHeader()) {
57
                    // Row is header.
58
                    setColRow(table, row.getCells(), colRowFont);
59
                } else {
60
                    // Row contains data.
61
                    setDataRow(table, row.getCells(), colRowFont, dataFont);
62
                }
63
            }
64

    
65
            // Open document and write created table into it.
66
            doc.open();
67
            doc.add(table);
68

    
69
            doc.close();
70
        } catch (Exception e) {
71

    
72
        }
73
    }
74

    
75
    /**
76
     * Set column header row.
77
     * @param table - Table.
78
     * @param cells - List of contingency table cells.
79
     * @param colRowFont - Column row styles.
80
     * @throws BadElementException If cell is not created successfully.
81
     */
82
    private static void setColRow(Table table, List<ContingencyTableRowCell> cells, Font colRowFont) throws BadElementException {
83

    
84
        for (ContingencyTableRowCell cell : cells) {
85

    
86
            // Create and prepare pdf cell.
87
            Cell pdfCell = prepareCell(cell.getValue(), colRowFont, cell.getColSpan());
88
            pdfCell.setHeader(true);
89
            table.addCell(pdfCell);
90
        }
91
    }
92

    
93
    /**
94
     * Set data row.
95
     * @param table - Table.
96
     * @param cells - List of contingency table cells.
97
     * @param colRowFont - Column row styles.
98
     * @param dataFont - Data row styles.
99
     * @throws BadElementException If cell is not created successfully.
100
     */
101
    private static void setDataRow(Table table, List<ContingencyTableRowCell> cells, Font colRowFont, Font dataFont) throws BadElementException {
102

    
103
        boolean headerWrited = false;
104

    
105
        for (ContingencyTableRowCell cell : cells) {
106

    
107
            // Create pdf cell.
108
            Cell pdfCell;
109

    
110
            // First cell is always header, so apply different style.
111
            if (!headerWrited) {
112

    
113
                headerWrited = true;
114

    
115
                // Prepare pdf cell.
116
                pdfCell = prepareCell(cell.getValue(), colRowFont, cell.getColSpan());
117
                pdfCell.setHeader(true);
118

    
119
            } else {
120

    
121
                // Prepare pdf cell.
122
                pdfCell = prepareCell(cell.getValue(), dataFont, cell.getColSpan());
123
            }
124

    
125
            table.addCell(pdfCell);
126
        }
127
    }
128

    
129
    /**
130
     * Prepare pdf cell.
131
     * @param value - Cell value.
132
     * @param font - Cell font.
133
     * @param colSpan - Cell colspan.
134
     * @return Prepared pdf cell.
135
     */
136
    private static Cell prepareCell(String value, Font font, int colSpan) {
137

    
138
        Cell cell = new Cell(new Phrase(value, font));
139
        cell.setColspan(colSpan);
140
        cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
141
        cell.setVerticalAlignment(VerticalAlignment.CENTER);
142

    
143
        return cell;
144
    }
145

    
146
    /**
147
     * Calculate column count in contingency table row.
148
     * @param firstRow - First row of contingency table.
149
     * @return Column count in contingency table.
150
     */
151
    private static int getColumnCount(ContingencyTableRow firstRow) {
152

    
153
        int columnCount = 0;
154

    
155
        for (ContingencyTableRowCell cell : firstRow.getCells()) {
156

    
157
            columnCount += cell.getColSpan();
158
        }
159

    
160
        return columnCount;
161
    }
162

    
163

    
164
    public static void exportt(File tmpFile, List<ContingencyTableRow> contingencyTableRows) {
165
        /*
166
        //Dummy Table
167
        float margin = 50;
168

    
169
        // starting y position is whole page height subtracted by top and bottom margin
170
        float yStartNewPage = myPage.getMediaBox().getHeight() - (2 * margin);
171

    
172
        // we want table across whole page width (subtracted by left and right margin ofcourse)
173
        float tableWidth = myPage.getMediaBox().getWidth() - (2 * margin);
174

    
175
        boolean drawContent = true;
176
        float yStart = yStartNewPage;
177
        float bottomMargin = 70;
178
        // y position is your coordinate of top left corner of the table
179
        float yPosition = 550;
180

    
181
        BaseTable table = new BaseTable(yPosition, yStartNewPage, bottomMargin, tableWidth, margin, mainDocument, myPage, true, drawContent);
182

    
183

    
184
        Row<PDPage> headerRow = table.createRow(15f);
185
        Cell<PDPage> cell = headerRow.createCell(100, "Header");
186
        table.addHeaderRow(headerRow);
187

    
188

    
189
        Row<PDPage> row = table.createRow(12);
190
        cell = row.createCell(30, "Data 1");
191
        cell = row.createCell(70, "Some value");
192

    
193
        table.draw();
194

    
195

    
196
        contentStream.close();
197
        mainDocument.addPage(myPage);
198
        mainDocument.save("testfile.pdf");
199
        mainDocument.close();
200
         */
201
    }
202
}
(3-3/5)