我正在使用 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 中的一个错误。
最佳答案
您是否期待此输出,否则请指定订单,我会尽力完成
我正在使用 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();
}
}
此问题的解决方案是将每个根列表项添加为单独的列表
关于java - itext:PdfPCell 中的嵌套列表显示不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22958063/