vandeseer / easytable

Small table drawing library built upon Apache PDFBox
MIT License
246 stars 94 forks source link

Problem with cell - one of them is empty #50

Closed pionas closed 5 years ago

pionas commented 5 years ago

My code:

        final TableBuilder tableBuilder = Table.builder()
                .addColumnsOfWidth(35, 92, 50, 50, 72, 240)
                .fontSize(8)
                .font(HELVETICA)
                .borderColor(Color.BLACK);

        tableBuilder.addRow(Row.builder()
                .add(TextCell.builder().text("XXXXXXXXX")
                        .colSpan(4)
                        .lineSpacing(1f)
                        .borderWidthTop(1)
                        .textColor(Color.BLACK)
                        .backgroundColor(Color.RED)
                        .fontSize(14)
                        .font(PDType1Font.HELVETICA_BOLD)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .add(TextCell.builder().text("YYY YYY")
                        .colSpan(2)
                        .textColor(Color.BLACK)
                        .backgroundColor(Color.RED)
                        .fontSize(11)
                        .font(PDType1Font.HELVETICA_BOLD)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .horizontalAlignment(LEFT)
                .build());

        tableBuilder.addRow(Row.builder()
                .add(TextCell.builder().text("#")
                        .rowSpan(3)
                        .lineSpacing(1f)
                        .borderWidthTop(1)
                        .textColor(Color.BLACK)
                        .backgroundColor(GRAY_LIGHT_3)
                        .fontSize(8)
                        .font(PDType1Font.HELVETICA_BOLD)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .add(TextCell.builder().text("CCC")
                        .rowSpan(3)
                        .textColor(Color.BLACK)
                        .backgroundColor(GRAY_LIGHT_3)
                        .fontSize(8)
                        .font(PDType1Font.HELVETICA_BOLD)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .add(TextCell.builder().text("QWERTY")
                        .colSpan(3)
                        .textColor(Color.BLACK)
                        .backgroundColor(Color.YELLOW)
                        .fontSize(10)
                        .font(PDType1Font.HELVETICA_BOLD)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .add(TextCell.builder().text("John Black")
                        .textColor(Color.BLACK)
                        .backgroundColor(Color.YELLOW)
                        .fontSize(8)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .horizontalAlignment(LEFT)
                .build());

        tableBuilder.addRow(Row.builder()
                .add(TextCell.builder().text("QQQQ")
                        .colSpan(3)
                        .lineSpacing(1f)
                        .borderWidthTop(1)
                        .textColor(Color.BLACK)
                        .backgroundColor(GRAY_LIGHT_3)
                        .fontSize(8)
                        .font(PDType1Font.HELVETICA_BOLD)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .add(TextCell.builder().text("ZZZZZ")
                        .rowSpan(2)
                        .lineSpacing(1f)
                        .borderWidthTop(1)
                        .textColor(Color.BLACK)
                        .backgroundColor(GRAY_LIGHT_3)
                        .fontSize(8)
                        .font(PDType1Font.HELVETICA_BOLD)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .horizontalAlignment(LEFT)
                .build());

        tableBuilder.addRow(Row.builder()
                .add(TextCell.builder().text("X")
                        .lineSpacing(1f)
                        .borderWidthTop(1)
                        .textColor(Color.BLACK)
                        .backgroundColor(GRAY_LIGHT_3)
                        .fontSize(8)
                        .font(HELVETICA)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .add(TextCell.builder().text("Y")
                        .lineSpacing(1f)
                        .borderWidthTop(1)
                        .textColor(Color.BLACK)
                        .backgroundColor(GRAY_LIGHT_3)
                        .fontSize(8)
                        .font(HELVETICA)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .add(TextCell.builder().text("LLLLLLLLLL")
                        .lineSpacing(1f)
                        .borderWidthTop(1)
                        .textColor(Color.BLACK)
                        .backgroundColor(GRAY_LIGHT_3)
                        .fontSize(8)
                        .font(HELVETICA)
                        .verticalAlignment(VerticalAlignment.MIDDLE)
                        .horizontalAlignment(CENTER)
                        .borderWidth(1)
                        .build())
                .horizontalAlignment(LEFT)
                .build());

        return tableBuilder.build();
vandeseer commented 5 years ago

Thanks for reporting, I will have a look.

pionas commented 5 years ago

I find the solution. https://github.com/vandeseer/easytable/blob/master/src/main/java/org/vandeseer/easytable/structure/Table.java#L112-L114

must change to:

for (int i = 0; i < cell.getRowSpan(); i++)
{
    if (rowSpanCells.contains(new Point(rows.size(), i)))
    {
        skipped++;
    }
}
vandeseer commented 5 years ago

Unfortunately this does not yet fix it. Try to run the test class ExcelLikeExampleTest. You will see that the second table is not rendered correctly now. But I think your change is pointing in the right direction :wink:

pionas commented 5 years ago

I think you can't skip first row's cell because the first row's cell can be the second or third element for another row's cell...

vandeseer commented 5 years ago

Should be fixed in develop branch now.

pionas commented 5 years ago

Now it's okey :) Good job :smile:

vandeseer commented 5 years ago

Fixed in release 0.5.2