javaitextpdfptable

Split tables into few pages


I have data that I insert into a table of kind of PdfPTable.
The data contains of data belongs to few sources, and I should split the data to few pages so that each source will start in a new page.

--- 1st data source data ---
page break
--- 2nd data source data ---
page break
--- 3rd data source data ---
page break

what is the best solution? It seems like PdfPTable doesn't have a function that does so.


Solution

  • This is usually done using separate PdfPTable instance. One table for the 1st data source data, the next table for the 2nd data source data, and so on. If you look at the examples of Chapter 4 of my book, you'll find an examples such as HeaderFooter1 and HeaderFooter2.

    In these examples, we get information about a film festival. This film festival spans several days. We want to list all the movies screened on a specific day in a table and we want to start a new page when a new day starts:

    List<Date> days = PojoFactory.getDays(connection);
    for (Date day : days) {
        document.add(getTable(connection, day));
        document.newPage();
    }
    

    The connection is a database connection, whereas day is the parameter we'll use to perform a query. The getTable() method create a new PdfPTable for each set of movies shown on a particular day.

    The page break you desire is achieved by invoking document.newPage() each time a PdfPTable with a certain data set has been added to the document.