【问题标题】:Nested large tables in itext7在 itext7 中嵌套大表
【发布时间】:2016-12-12 14:09:33
【问题描述】:

我已经看到在 itext 7 中处理大表的最佳方法在 http://developers.itextpdf.com/examples/tables/clone-large-tables 中进行了解释

我在另一个里面有一张桌子。如果大表是内表怎么办?

Table outTable = new Table(new float[]{1f},true);

Cell cellHeader1 = new Cell();
cellHeader1.add(new Paragraph("Header 1").addStyle(style));
outTable.addHeaderCell(cellHeader1);

document.add(outTable);

for (int i=0; i<smallArray.size();i++) {
    Table innerTable = new Table(new float[]{0.5f,0.5f},true);
    Cell cellHeader2 = new Cell(1,2);
    cellHeader2.add(new Paragraph("Header 2").addStyle(style));
    innerTable.addHeaderCell(cellHeader2);

    Cell cellInnerTable = new Cell();
    cellInnerTable.add(innerTable);
    outTable.addCell(cellInnerTable);

    for(int j=0;j<bigArray.size();j++){
        //add cells to innerTable;
        if (j%20==0){
            innerTable.flush();  (1)
            outTable.flush(); (2)
        }
    }

    innerTable.complete(); (1)
}
outTable.complete();

(2) 此刷新并不能解决内存问题。

(1) 这些行在 Table 对象的第 539 行返回 NullPointerException,因为 document 为空。 outTable 的父级是文档,因此“flush”方法将数据刷新到文档中,但 innerTable 的父级是 outTable,而不是文档。有没有办法将 innerTable 刷新到 outTable 中,将 outTable 刷新到文档中?

如果我将文档设置为 innerTable,那么我不会得到 NullPointerException: innerTable.setDocument(document);

它的行为不正常,因为现在 innerTable 被刷新到文档中,而不是被刷新到 outTable 中,并且它做了一些奇怪的事情。

非常感谢!

【问题讨论】:

    标签: java itext itext7


    【解决方案1】:

    不幸的是,只有将大表直接添加到Document 时才支持它们。不支持内部大表,并且没有计划在不久的将来添加对内部大表的支持。

    【讨论】:

    • 可惜了。谢谢!
    【解决方案2】:

    试试:

    for(int j=0;j<bigArray.size();j++){
        //add cells to innerTable;
        if (j%20==0){
            Cell cellContent = new Cell(1,2).add(innerTable);
            outTable.addCell(cellContent);
            innerTable.flushContent();  // API says is internal but is public and works OK
            outTable.flush();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多