vandeseer / easytable

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

ParagraphCell out of border #74

Closed buzzer83 closed 4 years ago

buzzer83 commented 4 years ago

Hi, when I insert a paragraph inside a table, sometimes the text, if too long, goes out of border. Attached is the code with a little example, the pom.xml file and the output. what am I doing wrong? Thank you

pom.xml.txt Report.java.txt example.pdf

vandeseer commented 4 years ago

Looks like a bug to me. From what I can see from a quick test it only happens if row spanning is active. I will look into it.

vandeseer commented 4 years ago

The problem here is that cells with row spanning do not have their "own" height – it will just be calculated by the other "regular" cells of the rows it spans.

That means: Because the two cells in the second column are not very high their sum does not exceed the height of the cell that spans two rows.

So what can you do about it? You could set the minHeight() of the cells on the right, e.g. like so:

private Table createRegularTable() {
    return Table.builder()
            .addColumnsOfWidth(50, 50)
            .addRow(Row.builder()
                    .add(TextCell.builder().minHeight(70).borderWidth(1).text("1").build())
                    .add(TextCell.builder().borderWidth(1).text("fu bza asd fad fda dsafa afa fsdfs fdsfs fds").rowSpan(2).build())
                    .build())
            .addRow(Row.builder()
                    .add(TextCell.builder().minHeight(70).borderWidth(1).text("2").build())
                    .build())
            .build();
}

What you also could do (but this is a bit hacky): calculate the height of the cell upfront by building a table containing only this cell and then set the minHeight() relative to this calculated value:

private Table createRegularTableWithCalculation() {
    TextCell textCell = TextCell.builder().borderWidth(1).text("fu bza asd fad fda dsafa afa fsdfs fdsfs fds").build();

    Table.builder()
            .addColumnsOfWidth(50)
            .addRow(Row.builder()
                    .add(textCell)
                    .build())
            .build();

    System.out.println(textCell.getHeight());

    Table realTable = Table.builder()
            .addColumnsOfWidth(50, 50)
            .addRow(Row.builder()
                    .add(TextCell.builder().minHeight(textCell.getHeight() / 2).borderWidth(1).text("1").build())
                    .add(textCell.toBuilder().rowSpan(2).build())
                    .build())
            .addRow(Row.builder()
                    .add(TextCell.builder().minHeight(textCell.getHeight() / 2).borderWidth(1).text("2").build())
                    .build())
            .build();

    return realTable;
}

Hope this helps.

Best, Stefan

buzzer83 commented 4 years ago

I tried the solution with the method that calculates the height. In the example I posted the solution works because the case is very simple. Now I have to try it in the project where the tables are dynamically generated. Thank you per the answer.

vandeseer commented 4 years ago

It's really an interesting case, I have to admit.

I honestly didn't think of this issue when I implemented row spanning (and believe me: supporting row spanning and column spanning at the same time creates a huge load of complexity which can easily be underestimated).

Since it would be really nice to have this feature I will try to look into it if I may find enough time.

vandeseer commented 4 years ago

I created a fix (which still needs a bit of testing) and I would like to ask you to try it out @buzzer83: It's in the branch bugfix/row-spanning-sizing. Could you install it locally and run your code with this version (i.e. 0.6.3-SNAPSHOT) and tell me if it worked for you?

PS: When installing locally just run mvn clean install -DskipTests -Dgpg.skip -Ddependency-check.skip=true (in order to speed things up).

buzzer83 commented 4 years ago

I installed version 0.6.3 locally (not with pom.xml) but it gives me several errors (look at them in the image). I certainly did something wrong. Immagine

vandeseer commented 4 years ago

You can install it locally by running the following commands:

git clone https://github.com/vandeseer/easytable.git
git checkout -b snapshot-branch origin/bugfix/row-spanning-sizing
mvn clean install -DskipTests -Dgpg.skip -Ddependency-check.skip=true

Afterwards you can reference the locally installed version in your pom.xml with version 0.6.3-SNAPSHOT.

PS: In your IDE you will get compile errors in case you don't have the Lombok plugin installed. But it's not necessary that you open it in your IDE.

buzzer83 commented 4 years ago

I installed the Lombok plugin and managed to resolve. your solution works correctly. TY Now I have to understand why the lines in paragraphs go wrong. For example in the third column. To indicate the end of the line I put the label "end". example.pdf

vandeseer commented 4 years ago

Glad to hear the solution works. For the other issue (or question) you're facing, please open another issue in order not to mix up things. Thanks.

buzzer83 commented 4 years ago

obviously. ty.

vandeseer commented 4 years ago

I will leave this issue open, because the fix is not yet released. As soon as version 0.6.3 is published, I'll close it.

Thanks for testing it upfront @buzzer83! 👍

vandeseer commented 4 years ago

Released with version 0.6.3.