【问题标题】:Repeated lines in PdfPTable at the end of the page?页面末尾 PdfPTable 中的重复行?
【发布时间】:2017-04-04 17:19:21
【问题描述】:

我正在使用 PdfPTable (iText) 打印一个填充了一些值列表的表格。

问题在于,在 PdfPTable 需要显示多个页面的情况下,它的最后一行打印在第一页的末尾,ALSO 打印在第二页的开头一个。

请在下面找到一个示例:


编辑:

请在下面找到代码:

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

【问题讨论】:

  • 你应该显示相关代码。
  • 这可能是一个非常旧的 iText 版本。这听起来像是多年前解决的问题。你用的是哪个版本?
  • @BrunoLowagie 我正在使用 iText 2.1.7。请注意,此功能是在我们的客户端站点中开发的,版本为 1.3.1,在该解决方案中不会出现此问题。
  • @GeomanYabes 我已经编辑了问题以包含代码
  • @BrunoLowagie 另外,我们正在使用这种依赖关系&lt;dependency&gt; &lt;groupId&gt;com.lowagie&lt;/groupId&gt; &lt;artifactId&gt;itext&lt;/artifactId&gt; &lt;version&gt;2.1.7&lt;/version&gt; &lt;/dependency&gt;

标签: itext pdfptable


【解决方案1】:

这个问题实际上在几年前就已经解决了(正如@Bruno 在评论中指出的那样)。

因此,解决方案是将使用的较旧 iText 版本替换为已修复问题的更新版本。

确实,OP 在其项目的另一个模块的 pom.xml 中有一个旧版本,该版本与他正在修改的模块冲突。他已经删除了它,现在它可以工作了。


从版本 2.x(包括非官方的 4.2.0)更新到 5.x 至少需要更新 import 语句,因为 itext 已从 com.lowagie 移动到 com.itextpdf。可能需要进一步更改以适应实际的 API 更改,例如在 5.3.x 版本中对签名 API 进行了大修;不过,基本 API 结构在 5.x 版本期间保持相当稳定。

此外,从低于 7 的任何版本更新到 7.x 都需要进行更大的更改,因为整个 iText API 已经过重新设计,以摆脱早期 API 设计的次优方面。

【讨论】:

  • 我必须指出 5.5.11 版本降低了解决方案的性能。事实上,在 2.1.7 版本中,我们有 bug(肯定),但执行时间是 4 小时以生成大约 100 000 个 PDF 文件,另一方面,在 5.5.11 版本中,错误已解决,但对于相同的 100 000 个 PDF 文件和相同的参数,执行时间现在是 5 小时半。
猜你喜欢
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
  • 2018-10-31
  • 1970-01-01
相关资源
最近更新 更多