itextpdfptable

Repeated lines in PdfPTable at the end of the page?


I'm using a PdfPTable (iText) to print a table that is populated with some list of values.

The problem is that, in the case where the PdfPTable takes more than one page to be displayed, its last line is printed at the end of the first page and ALSO at the beginning of the second one.

Please find an example below :

enter image description here


EDIT :

Please find the code below :

protected static PdfPTable addUserList(PdfWriter writer, Document document, List<MyObject> objects) throws Exception {

    PdfPTable headerTable = new PdfPTable(4);
    headerTable.setWidthPercentage(100);
    headerTable.setWidths(new int[] { 4, 7, 5, 3 });
    PdfPCell headerCell = PDFUtils.makeDefaultCell(1);
    headerCell.setBorderColor(Color.WHITE);
    headerCell.setBorder(PdfPCell.RIGHT);
    headerCell.setBorderWidth(1f);

    Phrase phrase = new Phrase("Column1", Style.OPIFICIO_12_BOLD_WHITE);
    headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    headerCell.setPhrase(phrase);
    headerTable.addCell(headerCell);

    phrase = new Phrase("Column2", Style.OPIFICIO_12_BOLD_WHITE);
    headerCell.setPhrase(phrase);
    headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    headerTable.addCell(headerCell);

    phrase = new Phrase("Column3", Style.OPIFICIO_12_BOLD_WHITE);
    headerCell.setPhrase(phrase);
    headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    headerTable.addCell(headerCell);

    phrase = new Phrase("Column4", Style.OPIFICIO_12_BOLD_WHITE);
    Chunk chunk = new Chunk("(1)", Style.OPIFICIO_6_BOLD_WHITE);
    chunk.setTextRise(7f);
    phrase.add(chunk);
    chunk = new Chunk("(XX)", Style.OPIFICIO_8_BOLD_WHITE);
    chunk.setTextRise(1f);
    phrase.add(chunk);
    headerCell.setPhrase(phrase);
    headerCell.setHorizontalAlignment(Element.ALIGN_CENTER);
    headerCell.setBorder(PdfPCell.NO_BORDER);
    headerTable.addCell(headerCell);

    PdfPTable userTable = new PdfPTable(4);
    userTable.setWidthPercentage(100);
    userTable.setWidths(new int[] { 4, 7, 5, 3 });
    PdfPCell cell = PDFUtils.makeDefaultCell(1);
    cell.setBackgroundColor(null);
    cell.setPaddingTop(2f);
    cell.setPaddingLeft(6f);
    cell.setPaddingRight(6f);

    for (MyObject object : objects) {

        if (object != null) {

            cell.setHorizontalAlignment(Element.ALIGN_LEFT);

            if (object.getAttribute1() != null) {
                phrase = new Phrase(object.getAttribute1(), Style.FUTURASTD_10_NORMAL_BLACK);
            } else {
                phrase = new Phrase("", Style.FUTURASTD_10_NORMAL_BLACK);
            }
            cell.setBorderWidth(1f);
            cell.setBorderColor(Color.WHITE);
            cell.setBorder(PdfPCell.RIGHT);
            cell.setPhrase(phrase);
            userTable.addCell(cell);

            phrase = new Phrase(object.getAttribute2(), Style.FUTURASTD_10_NORMAL_BLACK);
            cell.setBorderWidth(1f);
            cell.setBorderColor(Color.WHITE);
            cell.setBorder(PdfPCell.RIGHT);
            cell.setPhrase(phrase);
            userTable.addCell(cell);

            phrase = new Phrase(object.getAttribute3(), Style.FUTURASTD_10_NORMAL_BLACK);
            cell.setBorderWidth(1f);
            cell.setBorderColor(Color.WHITE);
            cell.setBorder(PdfPCell.RIGHT);
            cell.setPhrase(phrase);
            userTable.addCell(cell);

            phrase = new Phrase(object.getAttribute4(), Style.FUTURASTD_10_NORMAL_BLACK);
            cell.setBorder(PdfPCell.NO_BORDER);
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            cell.setPhrase(phrase);
            userTable.addCell(cell);

        }
    }

    PdfPTable mainTable = new PdfPTable(1);
    mainTable.setWidthPercentage(100);
    mainTable.setSplitLate(false);
    mainTable.setHeaderRows(1);
    PdfPCell cellH = new PdfPCell();
    cellH.addElement(headerTable);
    cellH.setBorder(Rectangle.NO_BORDER);
    cellH.setCellEvent(new PDFUtils.CellBackgroundRedRecap());
    mainTable.addCell(cellH);

    if (userTable.getRows().size() > 0) {
        PdfPCell cellUser = PDFUtils.makeDefaultCell(1);
        cellUser.setPaddingTop(7f);
        cellUser.setCellEvent(new PDFUtils.CellBackgroundRecap());
        cellUser.setBorder(PdfCell.NO_BORDER);
        cellUser.addElement(userTable);
        mainTable.addCell(cellUser);
    }

    return mainTable;
}

Solution

  • The problem actually was solved years ago (as @Bruno pointed out in a comment).

    The solution, therefore, is to replace the older iText version used by a more current one in which the problem is fixed.

    Indeed, the OP was having an old version in the pom.xml of another module of his project that was conflicting with the one he was modifying. He has deleted it and now it works.


    Updating from versions 2.x (including the unofficial 4.2.0) to 5.x requires at least updating import statements as itext was moved from com.lowagie to com.itextpdf. Further changes might be necessary to adapt to actual API changes, e.g. the signing API was overhauled during the 5.3.x versions; the basic API structure, though, remained fairly stable during the 5.x versions.

    Furthermore, updating from anything below 7 to 7.x requires greater changes as the whole iText API has been re-designed to get rid of sub-optimal aspects of the earlier API design.