repeatedheadertabledrawer不显示多个页面中的所有行

kninwzqo  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(215)

我正在尝试使用easytable库绘制一个大表(应该在多个页面中绘制)。
我使用repeatedheadertabledrawer类在每个新页面中重复表头。标题将按预期显示。但问题是,包含数据的表的其余部分没有正确显示:显示的表从应该显示的表的开头开始就没有变大(某些行丢失),最后一行也会重复,在所有数据行结束后,会显示更多重复的行,直到最后一页结束。我知道这是一个多方面的问题,但可能是一个调整可以解决所有问题。
创建表的方法如下所示:

private static Table createCalculationTable() {

    final  Color ORANGE_DARK = new Color(245, 120, 60, 243);
    final  Color YELLOW_LIGHT = new Color(226, 212, 75, 255);
    final  Color ORANGE_LIGHT = new Color(234, 147, 90, 220);

    final Table.TableBuilder tableBuilder = Table.builder()
            .addColumnsOfWidth(100, 100, 100, 100, 100)            
            .fontSize(8)
            .font(HELVETICA)
            .borderColor(WHITE);

    // add the header row
    tableBuilder.addRow(Row.builder()
            .add(TextCell.builder().text("Name").horizontalAlignment(LEFT).borderWidth(1).build())
            .add(TextCell.builder().text("First Number").borderWidth(1).build())
            .add(TextCell.builder().text("Second Number").borderWidth(1).build())
            .add(TextCell.builder().text("Third Number").borderWidth(1).build())
            .add(TextCell.builder().text("TOTAL").borderWidth(1).build())
            .backgroundColor(ORANGE_DARK)
            .textColor(WHITE)
            .font(HELVETICA_BOLD)
            .fontSize(9)
            .horizontalAlignment(CENTER)
            .build());

    // add data rows
    double grandTotal = 0;
    for (int i = 0; i < DATA.length; i++) {
        final Object[] dataRow = DATA[i];
        final double total = (double) dataRow[2] / 60 * (double) dataRow[3];
        grandTotal += total;

        tableBuilder.addRow(Row.builder()
                .add(TextCell.builder().text(String.valueOf(dataRow[0])).horizontalAlignment(LEFT).borderWidth(1).build())
                .add(TextCell.builder().text(String.valueOf(dataRow[1])).borderWidth(1).build())
                .add(TextCell.builder().text(String.valueOf(dataRow[2])).borderWidth(1).build())
                .add(TextCell.builder().text(dataRow[3] + " €").borderWidth(1).build())
                .add(TextCell.builder().text(total + " €").borderWidth(1).build())
                .backgroundColor(i % 2 == 0 ? YELLOW_LIGHT : ORANGE_LIGHT)
                .horizontalAlignment(RIGHT)
                .build());
    }

    // add the final row
    tableBuilder.addRow(Row.builder()
            .add(TextCell.builder().text("Total")
                    .colSpan(4)
                    .lineSpacing(1f)
                    .borderWidthTop(1)
                    .textColor(WHITE)
                    .backgroundColor(ORANGE_DARK)
                    .fontSize(9)
                    .horizontalAlignment(CENTER)
                    .font(HELVETICA_OBLIQUE)
                    .borderWidth(1)
                    .build())
            .add(TextCell.builder().text(grandTotal + " €").backgroundColor(ORANGE_DARK)
                    .font(HELVETICA_BOLD_OBLIQUE)
                    .verticalAlignment(TOP)
                    .borderWidth(1)
                    .build())
            .horizontalAlignment(RIGHT)
            .build());

    return tableBuilder.build();
}

数据表定义如下:

private final static Object[][] DATA = new Object[][]{
        {"BEGINNNN", 10.0, 150.0, 0.20},
        {"Location_1", 10.0, 150.0, 0.20},
        {"Location_11",   10.0, 200.0, 0.20},
        {"Location_111",    5.0, 200.0, 0.20},
        {"Location_1111",  5.0, 200.0, 0.20},
        {"Location_2",   10.0, 200.0, 0.20},
        {"Location_22",    5.0, 200.0, 0.20},
        {"Location_222",  5.0, 200.0, 0.20},
        {"Location_2222",   10.0, 200.0, 0.20},
        {"Location_3",    5.0, 200.0, 0.20},
        {"Location_33",  5.0, 200.0, 0.20},
        {"Location_333",   10.0, 200.0, 0.20},
        {"Location_3333",    5.0, 200.0, 0.20},
        {"Location_4",  5.0, 200.0, 0.20},
        {"Location_44",   10.0, 200.0, 0.20},
        {"Location_444",    5.0, 200.0, 0.20},
        {"Location_4444",  5.0, 200.0, 0.20},
        {"Location_5",   10.0, 200.0, 0.20},
        {"Location_55",    5.0, 200.0, 0.20},
        {"Location_555",  5.0, 200.0, 0.20},
        {"Location_5555",   10.0, 200.0, 0.20},
        {"Location_6",    5.0, 200.0, 0.20},
        {"Location_66",  5.0, 200.0, 0.20},
        {"Location_666",   10.0, 200.0, 0.20},
        {"Location_6666",    5.0, 200.0, 0.20},
        {"Location_66666",    5.0, 200.0, 0.20},
        {"Location_7",  5.0, 200.0, 0.20},
        {"Location_77",   10.0, 200.0, 0.20},
        {"Location_777",    5.0, 200.0, 0.20},
        {"Location_7777",  5.0, 200.0, 0.20},
        {"Location_77777",  5.0, 200.0, 0.20},
        {"Location_8",   10.0, 200.0, 0.20},
        {"Location_88",    5.0, 200.0, 0.20},
        {"Location_888",  5.0, 200.0, 0.20},
        {"Location_8888",   10.0, 200.0, 0.20},
        {"Location_88888",   10.0, 200.0, 0.20},
        {"Location_9",    5.0, 200.0, 0.20},
        {"Location_99",  5.0, 200.0, 0.20},
        {"Location_999",   10.0, 200.0, 0.20},
        {"Location_9999",    5.0, 200.0, 0.20},
        {"Location_99999",  5.0, 200.0, 0.20},
        {"Location_2",   10.0, 200.0, 0.20},
        {"Location_3",    5.0, 200.0, 0.20},
        {"Location_4",  5.0, 200.0, 0.20},
        {"Location_2",   10.0, 200.0, 0.20},
        {"Location_3",    5.0, 200.0, 0.20}, 
        {"Location_4",  5.0, 200.0, 0.20},   
        {"ENDDDDDD ",  5.0, 200.0, 0.20}
    };

以下方法调用createcalculationtable方法以插入创建的表:

public static void insertTable(String formPath, String formTargetPath) {

        try {
            PDDocument document = Loader.loadPDF(new File(formPath));
            final PDPage page = document.getPage(0);

            float startY = page.getMediaBox().getHeight() - PADDING;

            Table table = createCalculationTable();
            RepeatedHeaderTableDrawer.builder()           
                    .numberOfRowsToRepeat(1)
                    .page(page)
                    .table(table)                
                    .startX(PADDING )
                    .startY(startY )                             
                    .build()
                    .draw(() -> document, () -> page, PADDING);            
            document.save(formTargetPath);
            document.close();
        }
        catch (Exception e) {
            throw new ObjectNotFoundException("PDF file couldn't be created");
        }
    }

我检查了当数据正好适合一页时,整个表格将正确显示。但是,当第一页中只有一行不合适时,我会遇到下面提到的不同问题。我几乎可以肯定repeatedheadertabledrawer.builder()的.endy(值)可以解决在数据结束后重复行的问题。这意味着当没有更多数据时,表将结束,并且在最后一页结束之前不再显示冗余行,例如当前的情况。但是我不知道如何将这个值设置为“当没有更多的raw时”,因为表是可变的,并且没有固定的大小。如有任何想法/帮助,将不胜感激。谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题