forked from vandeseer/easytable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTableOverSeveralPagesTest.java
220 lines (180 loc) · 8.5 KB
/
TableOverSeveralPagesTest.java
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package org.vandeseer.integrationtest;
import de.redsix.pdfcompare.CompareResult;
import de.redsix.pdfcompare.PdfComparator;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.junit.Test;
import org.vandeseer.TestUtils;
import org.vandeseer.easytable.RepeatedHeaderTableDrawer;
import org.vandeseer.easytable.TableDrawer;
import org.vandeseer.easytable.settings.HorizontalAlignment;
import org.vandeseer.easytable.structure.Row;
import org.vandeseer.easytable.structure.Table;
import org.vandeseer.easytable.structure.cell.TextCell;
import java.awt.*;
import java.io.IOException;
import static junit.framework.TestCase.assertTrue;
import static org.vandeseer.TestUtils.getActualPdfFor;
import static org.vandeseer.TestUtils.getExpectedPdfFor;
public class TableOverSeveralPagesTest {
private static final String SEVERAL_PAGES_TABLE_1_FILE_NAME = "severalPagesTable1.pdf";
private static final String SEVERAL_PAGES_TABLE_2_FILE_NAME = "severalPagesTable2.pdf";
private static final String SEVERAL_PAGES_REP_HEADER_TABLE_1_FILE_NAME = "severalPagesTableRepeatedHeader.pdf";
private static final String SEVERAL_PAGES_REP_HEADER_TABLE_2_FILE_NAME = "severalPagesTableRepeatedHeaderMultipleRows.pdf";
private static final Color DARK_BLUE = new Color(46, 77, 97);
private static final Color CUSTOM_GRAY = new Color(136, 136, 136);
@Test
public void drawMultipageTable() throws IOException {
try (final PDDocument document = new PDDocument()) {
drawMultipageTableOn(document);
document.save(TestUtils.TARGET_FOLDER + "/" + SEVERAL_PAGES_TABLE_1_FILE_NAME);
}
final CompareResult compareResult = new PdfComparator<>(
getExpectedPdfFor(SEVERAL_PAGES_TABLE_1_FILE_NAME),
getActualPdfFor(SEVERAL_PAGES_TABLE_1_FILE_NAME)
).compare();
assertTrue(compareResult.isEqual());
}
@Test
public void drawSeveralMultipageTableOnSameDocument() throws IOException {
try (final PDDocument document = new PDDocument()) {
drawMultipageTableOn(document);
drawMultipageTableOn(document);
document.save(TestUtils.TARGET_FOLDER + "/" + SEVERAL_PAGES_TABLE_2_FILE_NAME);
}
final CompareResult compareResult = new PdfComparator<>(
getExpectedPdfFor(SEVERAL_PAGES_TABLE_2_FILE_NAME),
getActualPdfFor(SEVERAL_PAGES_TABLE_2_FILE_NAME)
).compare();
assertTrue(compareResult.isEqual());
}
@Test
public void createTwoPageTableWithRepeatedHeader() throws IOException {
try (final PDDocument document = new PDDocument()) {
RepeatedHeaderTableDrawer.builder()
.table(createTable())
.startX(50)
.startY(100F)
.endY(50F) // note: if not set, table is drawn over the end of the page
.build()
.draw(() -> document, () -> new PDPage(PDRectangle.A4), 50f);
document.save(TestUtils.TARGET_FOLDER + "/" + SEVERAL_PAGES_REP_HEADER_TABLE_1_FILE_NAME);
}
final CompareResult compareResult = new PdfComparator<>(
getExpectedPdfFor(SEVERAL_PAGES_REP_HEADER_TABLE_1_FILE_NAME),
getActualPdfFor(SEVERAL_PAGES_REP_HEADER_TABLE_1_FILE_NAME)
).compare();
assertTrue(compareResult.isEqual());
}
@Test
public void createTwoPageTableWithRepeatedHeaderOfThreeRows() throws IOException {
try (final PDDocument document = new PDDocument()) {
RepeatedHeaderTableDrawer.builder()
.table(createTableWithThreeHeaderRows())
.startX(50)
.startY(200F)
.endY(50F) // note: if not set, table is drawn over the end of the page
.numberOfRowsToRepeat(2)
.build()
.draw(() -> document, () -> new PDPage(PDRectangle.A4), 50f);
document.save(TestUtils.TARGET_FOLDER + "/" + SEVERAL_PAGES_REP_HEADER_TABLE_2_FILE_NAME);
}
final CompareResult compareResult = new PdfComparator<>(
getExpectedPdfFor(SEVERAL_PAGES_REP_HEADER_TABLE_2_FILE_NAME),
getActualPdfFor(SEVERAL_PAGES_REP_HEADER_TABLE_2_FILE_NAME)
).compare();
assertTrue(compareResult.isEqual());
}
private Table createTableWithThreeHeaderRows() {
final Table.TableBuilder tableBuilder = Table.builder()
.addColumnsOfWidth(200, 200);
tableBuilder
.addRow(Row.builder()
.add(createHeaderCell("Some"))
.add(createHeaderCell("Header"))
.build())
.addRow(Row.builder()
.add(TextCell.builder().text("This is a longer text that could be used to describe some " +
"data of the header. It is only used as a placeholder here.")
.colSpan(2)
.fontSize(6)
.horizontalAlignment(HorizontalAlignment.CENTER)
.padding(12)
.borderColor(DARK_BLUE)
.borderWidthBottom(2f)
.build())
.build());
for (int i = 0; i < 150; i++) {
tableBuilder.addRow(
Row.builder()
.add(TextCell.builder()
.text("Row " + i)
.textColor(CUSTOM_GRAY)
.borderColor(CUSTOM_GRAY)
.borderWidthBottom(2f)
.padding(12f)
.build())
.add(TextCell.builder()
.text("Value " + i)
.textColor(CUSTOM_GRAY)
.borderColor(CUSTOM_GRAY)
.borderWidthBottom(2f)
.padding(12f)
.build())
.build());
}
return tableBuilder.build();
}
private TextCell createHeaderCell(String text) {
return TextCell.builder()
.font(PDType1Font.HELVETICA_BOLD)
.text(text.toUpperCase())
.backgroundColor(DARK_BLUE)
.padding(16f)
.textColor(Color.WHITE)
.borderColor(Color.WHITE)
.borderWidth(2f)
.build();
}
private void drawMultipageTableOn(PDDocument document) throws IOException {
TableDrawer.builder()
.table(createTable())
.startX(50)
.startY(100F)
.endY(50F) // note: if not set, table is drawn over the end of the page
.build()
.draw(() -> document, () -> new PDPage(PDRectangle.A4), 50f);
}
private Table createTable() {
final Table.TableBuilder tableBuilder = Table.builder()
.addColumnOfWidth(200)
.addColumnOfWidth(200);
TextCell dummyHeaderCell = TextCell.builder()
.text("Header dummy")
.backgroundColor(Color.BLUE)
.textColor(Color.WHITE)
.borderWidth(1F)
.build();
tableBuilder.addRow(
Row.builder()
.add(dummyHeaderCell)
.add(dummyHeaderCell)
.build());
for (int i = 0; i < 50; i++) {
tableBuilder.addRow(
Row.builder()
.add(TextCell.builder()
.text("dummy " + i)
.borderWidth(1F)
.build())
.add(TextCell.builder()
.text("dummy " + i)
.borderWidth(1F)
.build())
.build());
}
return tableBuilder.build();
}
}