0
votes

How can i get the content height of a given page with pdfbox? I have X pages, where i need to add Y elements to. Once the first page is full, it should start a new page, putting out the rest of the Y elements and of course starting another page, once the second is also full.

So: How do i determine if the page is full - aka the content height?

I have something like this:

private static void addChapters() throws IOException {
    for(int i = 0; i < chapters.size(); i++) {

        // if page is full, add a new page and start printing on the new page here

        // Chapter Headline
        // some output here...

        // Data table
        // some more output here

        contentStream.close();
    }
}

Thanks!

1
I have X pages, where i need to add Y elements to. Once the first page is full, it should start a new page - PDFBox only implements a lowlevel API for PDF generation. This means that you have to provide the layouting code yourself. Usually this layouting code keeps track of where on the page it currently inserts content,and, therefore, knows when the page is full and starts a new one. Your code sceleton unfortunately misses these parts (some output here, some more output here). Thus, we can hardly tell you how that code can recognize that the page is full. - mkl

1 Answers

1
votes

PdfBox doesn´t have the tools to control page layout. What I´ve done at first was to multiply the number of results I wanted in a single page and their size to obtain the total height I was about to use. Then you can start writting in an Y higher than this result and change to a new page when the fixed number is reached.

However, at the end I was trying to print results like in a table, so I used Boxable for Pdfbox(https://github.com/dhorions/boxable) which handles page switching for you.