Java 表格单元格中的列表项未格式化
我对使用iText(版本5.5.2)生成的PDF有问题。我有一个表,其中应该包含各种元素,包括列表 但是,单元格内的列表显示错误-它根本没有呈现为列表,而是一个接一个地显示列表项 所以相反Java 表格单元格中的列表项未格式化,java,itext,tabular,Java,Itext,Tabular,我对使用iText(版本5.5.2)生成的PDF有问题。我有一个表,其中应该包含各种元素,包括列表 但是,单元格内的列表显示错误-它根本没有呈现为列表,而是一个接一个地显示列表项 所以相反 项目1 项目2 项目3 我得到 项目1项目2项目3 我正在使用以下代码: private static Paragraph list(String... items) { Paragraph para = new Paragraph(); com.itextpdf.text.List list
private static Paragraph list(String... items) {
Paragraph para = new Paragraph();
com.itextpdf.text.List list = new com.itextpdf.text.List(true, 10);
for (String item : items) {
list.add(new ListItem(item));
}
para.add(list);
return para;
}
document.add(list("item1","item2","item3));
PdfPTable table = new PdfPTable(2);
table.addCell("Some list");
table.addCell(list("item1","item2","item3));
document.add(table);
添加到表中的元素与添加到文档中的元素相同。区别在于,第一个正确显示为列表,第二个没有列表格式
我做错了什么?您正在文本模式下向
PdfPTable
添加列表。那是行不通的。您应该在复合模式下添加列表
。以下问题的答案解释了文本模式和复合模式之间的区别:
如果你想找到更多有用的答案来解释这两个概念之间的区别,请下载免费电子书(我在那里找到了上述问题的链接)
我还搜索了,这就是我如何找到示例的原因,该示例显示了将列表添加到PdfPCell
:
// We create a list:
List list = new List();
list.add(new ListItem("Item 1"));
list.add(new ListItem("Item 2"));
list.add(new ListItem("Item 3"));
// We wrap this list in a phrase:
Phrase phrase = new Phrase();
phrase.add(list);
// We add this phrase to a cell
PdfPCell phraseCell = new PdfPCell();
phraseCell.addElement(phrase);
// We add the cell to a table:
PdfPTable phraseTable = new PdfPTable(2);
phraseTable.setSpacingBefore(5);
phraseTable.addCell("List wrapped in a phrase:");
phraseTable.addCell(phraseCell);
// We wrap the phrase table in another table:
Phrase phraseTableWrapper = new Phrase();
phraseTableWrapper.add(phraseTable);
// We add these nested tables to the document:
document.add(new Paragraph("A list, wrapped in a phrase, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
document.add(phraseTableWrapper);
// This is how to do it:
// We add the list directly to a cell:
PdfPCell cell = new PdfPCell();
cell.addElement(list);
// We add the cell to the table:
PdfPTable table = new PdfPTable(2);
table.setSpacingBefore(5);
table.addCell("List placed directly into cell");
table.addCell(cell);
生成的PDF()与我预期的一样
但是,有两个警告:
- 该示例提到“此示例由Bruno Lowagie为潜在客户编写。此示例中的代码适用于最新版本的iText。它不适用于早于iText 5的版本”,我不知道它是否适用于iText 5.5.2
- 我知道嵌套列表在表中不受支持。因此,如果您需要单元格中列表中的列表,您将得到看起来与您希望的不一样的结果
您正在以文本模式将列表添加到PdfPTable
。那是行不通的。您应该在复合模式下添加列表
。以下问题的答案解释了文本模式和复合模式之间的区别:
如果你想找到更多有用的答案来解释这两个概念之间的区别,请下载免费电子书(我在那里找到了上述问题的链接)
我还搜索了,这就是我如何找到示例的原因,该示例显示了将列表添加到PdfPCell
:
// We create a list:
List list = new List();
list.add(new ListItem("Item 1"));
list.add(new ListItem("Item 2"));
list.add(new ListItem("Item 3"));
// We wrap this list in a phrase:
Phrase phrase = new Phrase();
phrase.add(list);
// We add this phrase to a cell
PdfPCell phraseCell = new PdfPCell();
phraseCell.addElement(phrase);
// We add the cell to a table:
PdfPTable phraseTable = new PdfPTable(2);
phraseTable.setSpacingBefore(5);
phraseTable.addCell("List wrapped in a phrase:");
phraseTable.addCell(phraseCell);
// We wrap the phrase table in another table:
Phrase phraseTableWrapper = new Phrase();
phraseTableWrapper.add(phraseTable);
// We add these nested tables to the document:
document.add(new Paragraph("A list, wrapped in a phrase, wrapped in a cell, wrapped in a table, wrapped in a phrase:"));
document.add(phraseTableWrapper);
// This is how to do it:
// We add the list directly to a cell:
PdfPCell cell = new PdfPCell();
cell.addElement(list);
// We add the cell to the table:
PdfPTable table = new PdfPTable(2);
table.setSpacingBefore(5);
table.addCell("List placed directly into cell");
table.addCell(cell);
生成的PDF()与我预期的一样
但是,有两个警告:
- 该示例提到“此示例由Bruno Lowagie为潜在客户编写。此示例中的代码适用于最新版本的iText。它不适用于早于iText 5的版本”,我不知道它是否适用于iText 5.5.2
- 我知道嵌套列表在表中不受支持。因此,如果您需要单元格中列表中的列表,您将得到看起来与您希望的不一样的结果
好的,所以我的问题是,我认为那张表。addCell(Element)与创建单元格、向单元格中添加元素并将单元格添加到表中是一样的……好的,我的问题是,我认为那张表。addCell(Element)与创建单元格、向单元格中添加元素并将单元格添加到表中是一样的。。。。