vandeseer / easytable

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

Bug with span width #7

Closed l33the closed 5 years ago

l33the commented 5 years ago

Since i cant make a pull request for whatever reason:

Theres a bug in the TableDrawer which causes glitches like this (replace this test with the actual one) :

    @Test
    public void createSampleDocumentWithCellSpanning() throws Exception {
        // Define the table structure first
        TableBuilder tableBuilder = TableBuilder.newBuilder()
                .addColumnOfWidth(300)
                .addColumnOfWidth(120)
                .addColumnOfWidth(70)
                .setFontSize(8)
                .setFont(HELVETICA);

        // Header ...
        tableBuilder.addRow(RowBuilder.newBuilder()
                .add(Cell.withText("This is right aligned without a border").setHorizontalAlignment(RIGHT))
                .add(Cell.withText("Sum")..setBackgroundColor(Color.ORANGE))
                .setBackgroundColor(Color.BLUE)
                .build());

        // Header ...
        tableBuilder.addRow(RowBuilder.newBuilder()
                .add(Cell.withText("This is right aligned without a border").span(2).setHorizontalAlignment(RIGHT))
                .setBackgroundColor(Color.BLUE)
                .build());

        createDocumentWithTable(tableBuilder.build(), "target/sampleWithCellSpanning.pdf");
    }

You see, adding span on the second cell in a row works. But on the first one it turns to a unwanted result.

Simply replace:

columnCounter++;

with

columnCounter += cell.getSpan()

in your drawBackgroundAndText and drawBorders.

Regards

vandeseer commented 5 years ago

Thanks a lot for reporting @TheSilentHorizon and thanks a lot for the fix as well! I will change it accordingly in the next days (and also check why you can't make a pull request)

l33the commented 5 years ago

@vandeseer i have to thank, your little TableAPI is a lifesaver 👍

vandeseer commented 5 years ago

Glad you find it useful :)

I changed the code accordingly and modified the test to include a row with cell spanning at the very beginning. Thanks again!

Please note that the version is still 0.0.xx, so I don't consider this being a mature library but something home-brew that is still in the making – hopefully though I will soon clean the API a bit, i.e. having more consistent method names etc. and bring it to 0.xx.xx and maybe adding breaking of text, row spanning, adding of images etc. one day as well ...