【发布时间】:2021-08-24 01:22:28
【问题描述】:
我正在尝试使用 vandeseer 的 easytable 创建一个带有标题行的表,然后使用 for 循环创建多个行。
我能做的最好的事情是创建两个单独的表格并将它们相互对齐。第一个表是标题行,第二个表是我希望 for 循环填充多行的地方。这是使用 PDFBox 渲染时的样子。
我当前的代码:
private void drawActiveCelUserRolesTable(PDPageContentStream contentStream, List<CelUser> users) {
//draw table header
Table activeUsersHeader = Table.builder()
.addColumnsOfWidth(100,100,100,100,100)
.addRow(Row.builder()
.add(TextCell.builder().text("Last Name").borderWidth(1).build())
.add(TextCell.builder().text("First Name").borderWidth(1).build())
.add(TextCell.builder().text("Role Name").borderWidth(1).build())
.add(TextCell.builder().text("Effective Date").borderWidth(1).build())
.add(TextCell.builder().text("Expiry Date").borderWidth(1).build())
.build())
.build();
TableDrawer headerDrawer = TableDrawer.builder()
.contentStream(contentStream)
.startX(HEADER_TABLE_LOCATION_X)
.startY(HEADER_TABLE_LOCATION_Y-50)
.table(activeUsersHeader)
.build();
headerDrawer.draw();
Table activeUsersRows = Table.builder()
.addColumnsOfWidth(100,100,100,100,100)
.addRow(Row.builder()
.add(TextCell.builder().text("Joe").borderWidth(1).build())
.add(TextCell.builder().text("Bob").borderWidth(1).build())
.add(TextCell.builder().text("ADM").borderWidth(1).build())
.add(TextCell.builder().text("17/12/20").borderWidth(1).build())
.add(TextCell.builder().text("17/12/21").borderWidth(1).build())
.build())
.build();
TableDrawer rowDrawer = TableDrawer.builder()
.contentStream(contentStream)
.startX(HEADER_TABLE_LOCATION_X)
.startY(HEADER_TABLE_LOCATION_Y - 67)
.table(activeUsersRows)
.build();
rowDrawer.draw();
}
我尝试使用 for 循环来填充行,如下所示,但它显然不起作用。
Table activeUsersRows = Table.builder()
.addColumnsOfWidth(100,100,100,100,100)
for (int i = 0; i < 5; i++) {
.addRow(Row.builder()
.add(TextCell.builder().text("Joe").borderWidth(1).build())
.add(TextCell.builder().text("Bob").borderWidth(1).build())
.add(TextCell.builder().text("ADM").borderWidth(1).build())
.add(TextCell.builder().text("17/12/20").borderWidth(1).build())
.add(TextCell.builder().text("17/12/21").borderWidth(1).build())
.build())
.build();
}
我已尝试查看文档,但似乎找不到类似的内容。我见过使用 Table.TableBuilder 的示例,但由于某种原因,这些表不会在 pdf 上呈现。因此我只使用了 Table 类。
【问题讨论】: