ralfstuckert / pdfbox-layout

MIT License
155 stars 74 forks source link

how to detect the end of the page when not using exact positioning #63

Open arnthom opened 6 years ago

arnthom commented 6 years ago

Hello Ralf!

I have been successful in using PdfBox-Layout with PdfBox 2 to replace iText. :) First I'd like to say "thank you" for this nice software.

But some things are still at work. When I add text elements to the document, things like paging are done automatically. BUT, how do I recognize when I'm near to the end of the page? I dont like to put a heading in the last line of the page and the associated text goes to the next page. Exact positioning of the text elements is not possible, because the text is built out of some fragments depeding on lots of variables and other curcumstances ...

Is there any possibility to define, that a paragraph should not be split on two pages? May .divide(float, float) help in that case? Is there any example for that available? This would help a lot.

Thanks again!

ralfstuckert commented 6 years ago

Hi, at first sorry for delays. Actually I don't have much time to spend on the library :-|

What you demand is currently not built-in. But the image does something like this. So as a workaround you may create an extension of Paragraph that overwrites divide() as follows:

@Override
    public Divided divide(float remainingHeight, float nextPageHeight)
        throws IOException {
    if (getHeight()<= nextPageHeight) {
        return new Divided(new VerticalSpacer(remainingHeight), this);
    }
    return super.divide(remainingHeight, nextPageHeight);
    }

Haven't tried it out, but this should do the trick: If there is not enough space on the current page but on the next page, add a vertical space, so the paragraph starts at the next page.

arnthom commented 6 years ago

Well, this seems to work, first test was successful. Now I have to put the text elements that shoould not be split by a page break into one extended Paragraph.

Thanks a lot!

arnthom commented 6 years ago

I need to discuss a similar case: I want to add some text elements, that use my table-workaround with column layout. If there is less space than half the page height, I need a page break (ControlElement.NEWPAGE). How do I check the available vertical space? The workaround above just checks at rendering time, if there is enough space to add this paragraph, but I do not see, how to use this for a more complex text (that consist of some paragraphs and layout changes) ... until there is a nested layout approach ...