java - itext:PdfPCell 中的嵌套列表显示不完整

标签 java itext

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

这是演示该问题的代码:

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..),我发现如果列表的最后一项是另一个列表(不是 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 中的一个错误。

最佳答案

您是否期待此输出,否则请指定订单,我会尽力完成

enter image description here

我正在使用 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();

        }

    }

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

enter image description here

Pdf

关于java - itext:PdfPCell 中的嵌套列表显示不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22958063/

相关文章:

java - 使用 iText PDF 删除 PDF 中的页眉和页脚时出现问题

java - iText:将现有 PDF 的颜色更改为灰度

java - 从点对象获取值,该对象在 Java 中用作圆对象的中心

java - 使用 Getter Setter JAVA 访问其他类的变量

java - Hibernate 搜索突出显示未分析的字段

java - 如何在action类和jsp页面之间传递对象数据?

java - 使用 JAVA 搜索和替换 PDF 中的文本

java - iText/Swing : Breaking text in a box

java - com.itextpdf.test.** 类的用途是什么?

java - 无法为方法 public Abstract java.util.Optional 创建查询元模型