java 如何在EasyTable中创建此格式的表

flvlnr44  于 2023-04-04  发布在  Java
关注(0)|答案(2)|浏览(125)

我想有一个单一的标题在我的情况下笔记行spaning到多行(2)。

rg.vandeseer.easytable.structure.Table$TableBuilder$TableSetupException: Number of table cells does not match with table setup. This could be due to row or col spanning not being correct. at org.vandeseer.easytable.structure.Table$TableBuilder.build(Table.java:223) ~[easytable-0.8.5.jar:na] at com.fedex.airops.pilot.bank.util.pdf.RollOverPDFAAPB.createSimplePDF(RollOverPDFAAPB.java:79) ~[classes/:na]

有人可以告诉我,我如何才能代码为给定的样本表使用易表

mwg9r5ms

mwg9r5ms1#

下面是上面结构的一个最小工作示例:

try (PDDocument document = new PDDocument()) {
    final PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);

    try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {

        // Build the table
        Table myTable = Table.builder()
                .addColumnsOfWidth(20, 20, 20, 20, 20, 20, 20)
                .padding(2)
                .borderWidth(1)
                .borderColor(Color.gray)
                .horizontalAlignment(HorizontalAlignment.CENTER)
                .addRow(Row.builder()
                        .add(TextCell.builder().text("a").colSpan(7).build())
                        .build())
                .addRow(Row.builder()
                        .add(TextCell.builder().text("b").rowSpan(2).build())
                        .add(TextCell.builder().text("c").colSpan(3).build())
                        .add(TextCell.builder().text("d").colSpan(3).build())
                        .build())
                .addRow(Row.builder()
                        .add(TextCell.builder().text("e").build())
                        .add(TextCell.builder().text("f").build())
                        .add(TextCell.builder().text("g").build())
                        .add(TextCell.builder().text("h").build())
                        .add(TextCell.builder().text("i").build())
                        .add(TextCell.builder().text("j").build())
                        .build())
                .build();

        // Set up the drawer
        TableDrawer tableDrawer = TableDrawer.builder()
                .contentStream(contentStream)
                .startX(20f)
                .startY(page.getMediaBox().getUpperRightY() - 20f)
                .table(myTable)
                .build();

        // And go for it!
        tableDrawer.draw();
    }

    document.save("example.pdf");
}
9jyewag0

9jyewag02#

这是我的PDF生成代码,里面有一个表格

Set<TransferReqItem> transferReqItems = transferRequest.getItems();
        if (transferReqItems != null && !transferReqItems.isEmpty()) {
            List<String> headerNames = new ArrayList<>(Arrays.asList("#", "Item", "Unit Type", "TO Requested Quantity", "Issue quantity", "Reason"));
            int[] columnWidths = new int[]{5, 25, 20, 10, 15, 20};
            reportRows = createTableWithTableHeaders(reportRows, headerNames, columnWidths);
            Integer index = 0;

你可以看到有6库仑,所以我需要给予6行数据

否则将触发异常。
下面是如何给予数据行:

tableDataRow.getRowData().add(new CellData(String.valueOf(index)));
                tableDataRow.getRowData().add(new CellData(TESTDATA);
                tableDataRow.getRowData().add(new CellData(TESTDATA);
                tableDataRow.getRowData().add(new CellData(TESTDATA));
                tableDataRow.getRowData().add(new CellData(TESTDATA);
                tableDataRow.getRowData().add(new CellData(TESTDATA);

相关问题