0

PdfPTable (iText) を使用して、値のリストが入力されたテーブルを印刷しています。

問題は、PdfPTable の表示に複数のページが必要な場合、その最後の行が最初のページの最後に印刷され、2 ページ目の最初にも印刷されることです

以下に例を示します。

ここに画像の説明を入力


編集 :

以下のコードを見つけてください:

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;
}
4

1 に答える 1