Created
August 5, 2019 13:04
-
-
Save vandeseer/b1455419486b16b757494ae8aa408502 to your computer and use it in GitHub Desktop.
Another performance test for creating PDF documents with tables using easytable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void perfTest4() throws IOException { | |
String outputFileName = "perfTest4.pdf"; | |
final PDDocument document = new PDDocument(); | |
java.util.List<Object[]> dataList = new ArrayList<>(); | |
for (int i = 0; i < 5000; i++) { | |
dataList.add(new Object[]{"Whisky", 134.0, 145.0}); | |
dataList.add(new Object[]{"Beer", 768.0, 677.0}); | |
dataList.add(new Object[]{"Gin", 456.2, 612.0}); | |
dataList.add(new Object[]{"Vodka", 302.3, 467.0}); | |
} | |
RepeatedHeaderTableDrawer drawer = RepeatedHeaderTableDrawer.builder() | |
.table(createSimpleExampleTable(dataList.toArray(new Object[][]{}))) | |
.startX(30) | |
.startY(775F) | |
.endY(25F) // note: if not set, table is drawn over the end of the page | |
.build(); | |
//for (int i = 0; i < 400; i++) { | |
do { | |
PDPage page = new PDPage(new PDRectangle(1150,800)); | |
document.addPage(page); | |
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) { | |
drawer.contentStream(contentStream).draw(); | |
} | |
drawer.startY(page.getMediaBox().getHeight() - 50); //50 is each page height | |
} while (!drawer.isFinished()); | |
//} | |
document.save(TARGET_FOLDER + "/" + outputFileName); | |
document.close(); | |
} | |
private Table createSimpleExampleTable(Object[][] data) { | |
// Define the table structure first | |
final Table.TableBuilder tableBuilder = Table.builder() | |
.addColumnsOfWidth(100, 50, 50, 50) | |
.fontSize(8) | |
.font(HELVETICA) | |
.borderColor(Color.WHITE); | |
// Add the header row ... | |
final Row headerRow = Row.builder() | |
.add(TextCell.builder().text("Product").horizontalAlignment(LEFT).borderWidth(1).build()) | |
.add(TextCell.builder().text("2018").borderWidth(1).build()) | |
.add(TextCell.builder().text("2019").borderWidth(1).build()) | |
.add(TextCell.builder().text("Total").borderWidth(1).build()) | |
.backgroundColor(BLUE_DARK) | |
.textColor(Color.WHITE) | |
.font(PDType1Font.HELVETICA_BOLD).fontSize(9) | |
.horizontalAlignment(CENTER) | |
.build(); | |
tableBuilder.addRow(headerRow); | |
// ... and some data rows | |
double grandTotal = 0; | |
for (int i = 0; i < data.length; i++) { | |
final Object[] dataRow = data[i]; | |
final double total = (double) dataRow[1] + (double) dataRow[2]; | |
grandTotal += total; | |
tableBuilder.addRow(Row.builder() | |
.add(TextCell.builder().text(String.valueOf(dataRow[0])).horizontalAlignment(LEFT).borderWidth(1).build()) | |
.add(TextCell.builder().text(dataRow[1] + " €").borderWidth(1).build()) | |
.add(TextCell.builder().text(dataRow[2] + " €").borderWidth(1).build()) | |
.add(TextCell.builder().text(total + " €").borderWidth(1).build()) | |
.backgroundColor(i % 2 == 0 ? BLUE_LIGHT_1 : BLUE_LIGHT_2) | |
.horizontalAlignment(RIGHT) | |
.build()) | |
.wordBreak(true); | |
} | |
// Add a final row | |
tableBuilder.addRow(Row.builder() | |
.add(TextCell.builder().text("This spans over 3 cells, is right aligned and its text is so long that it even breaks. " + | |
"Also it shows the grand total in the next cell and furthermore vertical alignment is shown:") | |
.colSpan(3) | |
.lineSpacing(1f) | |
.borderWidthTop(1) | |
.textColor(WHITE) | |
.backgroundColor(BLUE_DARK) | |
.fontSize(6) | |
.font(HELVETICA_OBLIQUE) | |
.borderWidth(1) | |
.build()) | |
.add(TextCell.builder().text(grandTotal + " €").backgroundColor(LIGHT_GRAY) | |
.font(HELVETICA_BOLD_OBLIQUE) | |
.verticalAlignment(VerticalAlignment.TOP) | |
.borderWidth(1) | |
.build()) | |
.horizontalAlignment(RIGHT) | |
.build()); | |
return tableBuilder.build(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment