【问题标题】:itext: Nested lists in PdfPCell are shown incompleteitext:PdfPCell 中的嵌套列表显示不完整
【发布时间】:2014-04-09 09:20:00
【问题描述】:

我正在使用 iText 2.1.7 生成 PDF 文档。我有嵌套列表的问题。将嵌套列表添加到 Document 时,它可以正常工作。但是,如果将相同的嵌套列表添加到本身是 PdfPTable 一部分的 PdfPCell 中,则顶部列表中的某些项目会丢失。是我的代码不正确还是这是错误?

这是一个演示问题的代码:

com.lowagie.text.List sublistEOS = new com.lowagie.text.List(com.lowagie.text.List.UNORDERED, 10);
sublistEOS.add(new ListItem("D60"));
sublistEOS.add(new ListItem("D70"));
com.lowagie.text.List sublistPowerShot = new com.lowagie.text.List(com.lowagie.text.List.UNORDERED, 10);
sublistPowerShot.add(new ListItem("G15"));
sublistPowerShot.add(new ListItem("GX"));
com.lowagie.text.List sublistC = new com.lowagie.text.List(com.lowagie.text.List.UNORDERED, 10);
sublistC.add(new ListItem("EOS"));
sublistC.add(sublistEOS);
sublistC.add(new ListItem("Powershot"));
sublistC.add(sublistPowerShot);
com.lowagie.text.List list = new  com.lowagie.text.List(com.lowagie.text.List.UNORDERED, 10);
list.add(new ListItem("Canon"));
list.add(sublistC);
list.add(new ListItem("Nikon"));

//this works well
document.add(list);

//this doesn't work well - list item Nikon is missing!
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
cell.addElement(list);
document.add(table)

更新: 问题是,在实际代码中,我不是自己创建列表,而是通过调用

HTMLWorker.parseToList(new StringReader(html), styleSheet)

其中 html 是包含任何 HTML 的字符串(在我的例子中它包含嵌套列表) 所以我不能轻易影响生成列表的结构(这对我来说看起来不错,直接添加到文档时效果很好) 我一直在玩弄我的示例代码(Canon,Nicon..),我发现如果 List 的最后一项是另一个 List(不是 ListItem),就会出现问题。所以我编写了一个“更正”递归方法,它接受一个 List 并在需要时添加一个假 ListItem:

private void processList(com.lowagie.text.List aList) {
    ListItem fakeLI = new ListItem();
    List items = aList.getItems();
    for(int i = 0; i < items.size(); i++) {
        Object item = items.get(i);
        if (item instanceof com.lowagie.text.List) {
            processList((com.lowagie.text.List)item);//recursive call
            if (i + 1 == items.size()) {
                items.add(fakeLI);
            }
        }
    }
}

所以在我的示例代码中我调用:

cell.addElement(processList(list));

这“似乎”有效,但也许有更好的工作。我倾向于认为这是 iText 中的一个错误。

【问题讨论】:

  • 不是 iText 中的错误。它 iText 中的一个错误。您使用的是 2009 年 7 月发布的过时版本。您提到的错误已解决。请不要报告已修复的错误!请改为升级到最新版本的 iText。
  • 好的,感谢您提供此信息。所以这是一个错误,它已被修复(我怎么知道?)。不幸的是,由于许可原因,我无法迁移到更新版本的 iText - 如果我没记错的话,2.1.7 仍在 LPGL 之下,而 5.0 及更高版本则不是:-(我的公司管理层拒绝付款商业许可证,这样我们就不必发布我们的专有代码,所以我永远坚持使用旧的 iText:-(
  • 您的公司管理层可能基于错误的论点做出了决定。请观看我们的一位客户解释好处的视频:youtube.com/watch?v=uzF8E4h7Evo 我们刚刚完成了一项调查,客户(包括贵公司的客户)不希望使用过时版本的 iText 创建 PDF。他们希望您使用新版本。定价过高的论点通常是基于误解。只需与 iText 联系,也许我们可以帮助您说服您的管理层您应该获得应得的工具。

标签: java itext


【解决方案1】:

您是否期望这个输出如果否则请指定订单,我会尽力完成它

我正在使用 com.itextpdf 包

    package test1;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;

    import com.itextpdf.*;
    import com.itextpdf.text.Document;
    import com.itextpdf.text.DocumentException;
    import com.itextpdf.text.List;
    import com.itextpdf.text.ListItem;
    import com.itextpdf.text.pdf.PdfPCell;
    import com.itextpdf.text.pdf.PdfPTable;
    import com.itextpdf.text.pdf.PdfWriter;
    public class JavaIText {

        /**
         * @param args
         * @throws DocumentException 
         * @throws IOException 
         */
        public static void main(String[] args) throws DocumentException, IOException {
            // TODO Auto-generated method stub
             OutputStream file = new FileOutputStream(new File("D:\\PDF_Java4s.pdf"));
            Document document =new Document();
             PdfWriter.getInstance(document, file);
            com.itextpdf.text.List sublistEOS = new com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED, 10);

            sublistEOS.add(new ListItem("D60"));
            sublistEOS.add(new ListItem("D70"));

            com.itextpdf.text.List sublistPowerShot = new com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED, 10);

            sublistPowerShot.add(new ListItem("G15"));
            sublistPowerShot.add(new ListItem("GX"));

            com.itextpdf.text.List sublistC = new com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED, 10);

            sublistC.add(new ListItem("EOS"));
            sublistC.add(sublistEOS);

            sublistC.add(new ListItem("Powershot"));
            sublistC.add(sublistPowerShot);

            com.itextpdf.text.List list = new  com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED, 10);
            list.add(new ListItem("Canon"));

            list.add(sublistC);
            com.itextpdf.text.List list1 = new  com.itextpdf.text.List(com.itextpdf.text.List.UNORDERED, 10);

            list1.add(new ListItem("Nikon"));


             document.open();
            //this works well
            document.add(list);


            PdfPTable table = new PdfPTable(1);
            PdfPCell cell = new PdfPCell();
            cell.addElement(list);

为了将 Nokion 显示为第二项,请将其添加为单独的列表

            cell.addElement(list1);
            cell.setPaddingBottom(8);

            table.addCell(cell);
            document.add(table);
            document.close();

            file.close();

        }

    }

解决这个问题的方法是将每个根列表项添加为单独的列表

【讨论】:

  • 好的,这就是你想要的
  • 是的,我期待屏幕截图中的输出。我只有在 List 直接添加到 Document 时才得到它。如果将 List 添加到表中,则不会呈现“Nikon”。我用 iText 5.5.0 测试了相同的代码,但结果是一样的:-(
  • 嘿,我在运行代码时遇到了同样的问题,解决方法是在 cannon (List) 下添加每个子列表,然后在这种情况下创建另一个列表项 List1 或您希望输入的任何其他名称并添加它到 pdftable 的单元格中,我添加了两个单元格以显示它可以正常工作
  • @user3514427 使用的是过时的 iText 版本。当该错误已在很久以前修复时,说“iText 中存在错误”是不公平的。我赞成这个答案,我建议 user3514427 接受它。
  • 干杯我想知道它工作正常是什么没注意到
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
相关资源
最近更新 更多