表格单元格中的列表项目未格式化 [英] List items in table cell are not formatted

查看:121
本文介绍了表格单元格中的列表项目未格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了使用iText(版本5.5.2)生成的PDF问题。我有一个表应该包含各种元素,包括列表。

I have a problem with PDF, generated using iText (version 5.5.2). I have a table which should contain various elements, including lists.

然而,单元格内的列表被错误地显示 - 它根本不作为列表呈现,而是列出项目一个接一个地显示。

However, the list inside the cell is wrongly displayed - it's not rendered at all as list, instead list items are displayed one by another.

所以改为



  1. item1

  2. item2

  3. item3


我得到了


item1item2item3

item1item2item3

我正在使用以下代码:

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);

添加到表中的元素与添加到文档中的元素相同。区别在于,第一个正确显示,列表,第二个没有列表格式。

The element that is added to the table is identical to that that is added to the document. The difference is, first is displayed correctly, as list, and second has no list formatting.

我在这里做错了什么?

推荐答案

您要将列表添加到 PdfPTable 文本模式。这永远不会奏效。您应该在复合模式中添加列表文本模式复合模式之间的区别在以下问题的答案中进行了解释:

You are adding a List to a PdfPTable in text mode. That can never work. You should add the List in composite mode. The difference between text mode and composite mode is explained in the answer to the following questions:

如果您想找到解释这两个概念之间差异的更多有用答案,请下载免费的电子书 StackOverflow上最好的iText问题(这是我找到上述问题链接的地方)。

If you want to find more useful answers explaining the difference between these two concepts, please download the free ebook The Best iText Questions on StackOverflow (that's where I found the link to the above questions).

我也搜索了 sandbo在官方iText网站上的示例,这就是我发现 ListInCell 示例的方式将列表添加到 PdfPCell 的许多不同方法:

I've also searched the sandbox examples on the official iText website and that's how I found the ListInCell example showing many different ways to add a list to a 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( list_in_cell.pdf )看起来就像我期望的那样。

The resulting PDF (list_in_cell.pdf) looks the way I'd expect it.

然而,有两个警告:


  • 示例提及此示例由Bruno Lowagie为潜在客户编写。此示例中的代码适用于iText的最新版本。它不适用于iText 5之前的版本,我不知道它是否适用于iText 5.5.2。

  • 我知道表中不支持嵌套列表。因此,如果您需要单元格内的列表中的列表,您将获得看起来不像您想要的结果。

  • The example mentions "This example was written by Bruno Lowagie for a prospective customer. The code in this sample works with the latest version of iText. It doesn't work with versions predating iText 5" and I don't know if it will work with iText 5.5.2.
  • I know that nested lists aren't supported in tables. So if you need a list inside a list inside a cell, you'll get results that don't look the way you want it.

这篇关于表格单元格中的列表项目未格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆