Search code examples
javaitext

How To Set Data Per Page Itextpdf Java


I made an export pdf system using the java Itext pdf library, but there is a problem when I want to split the data of each page, I want to dynamically divide the data of each page using input, how to do it?like the code below, but the result is not as expected

//looping data row
        PdfPTable itemTable = new PdfPTable(headerColumns.length);
        itemTable.setWidthPercentage(100);
        for(int i = 0;i< data.size();i++){

            // header area
            PdfPCell area = new PdfPCell(new Phrase(data.get(i).get("area").toString()));
            area.setPaddingLeft(4);
            area.setVerticalAlignment(Element.ALIGN_MIDDLE);
            area.setHorizontalAlignment(Element.ALIGN_CENTER);
            itemTable.addCell(area);

            // header new_sa
            PdfPCell newSa = new PdfPCell(new Phrase(data.get(i).get("new_sa").toString()));
            newSa.setPaddingLeft(4);
            newSa.setVerticalAlignment(Element.ALIGN_MIDDLE);
            newSa.setHorizontalAlignment(Element.ALIGN_CENTER);
            itemTable.addCell(newSa);

            // header new_sa
            PdfPCell saMtd = new PdfPCell(new Phrase(data.get(i).get("sa_mtd").toString()));
            saMtd.setPaddingLeft(4);
            saMtd.setVerticalAlignment(Element.ALIGN_MIDDLE);
            saMtd.setHorizontalAlignment(Element.ALIGN_CENTER);
            itemTable.addCell(saMtd);

            // header sa_last_month
            PdfPCell saLastMonth = new PdfPCell(new Phrase(data.get(i).get("sa_last_month").toString()));
            saLastMonth.setPaddingLeft(4);
            saLastMonth.setVerticalAlignment(Element.ALIGN_MIDDLE);
            saLastMonth.setHorizontalAlignment(Element.ALIGN_CENTER);
            itemTable.addCell(saMtd);

            //set maxmimum data per page 2
            if(itemTable.getRows().size() == 2){
                document.newPage();
            }
            document.add(table);
            document.add(itemTable);
        }
        document.close();

pdf result of the code above picture page 1 picture page 2


Solution

  • In a comment you clarified

    the results that I expect, such as data per page for example 2 , in the results of the image I include it is like a bug where the data on the initial page is 1 but the data is repeated on the 2nd page

    If you don't want the data to repeat, then you should not add the same itemTable object again and again but instead create a new one at the start of the loop.

    I.e. you should pull the two lines into the loop that currently are before the loop, instead of

    PdfPTable itemTable = new PdfPTable(headerColumns.length);
    itemTable.setWidthPercentage(100);
    for(int i = 0;i< data.size();i++){
        ...
        document.add(itemTable);
    }
    

    use

    for(int i = 0;i< data.size();i++){
        PdfPTable itemTable = new PdfPTable(headerColumns.length);
        itemTable.setWidthPercentage(100);
        ...
        document.add(itemTable);
    }