如何将Label设置为iText List

时间:2014-02-05 13:50:36

标签: java itext

我正在尝试实现以下结构:

Label: - Value 1
       - Value 2    
       - Value 3

这是我的代码:

document.add(new Chunk("Label: ", BOLD));
List list = new List(List.UNORDERED);           
list.add(new ListItem(new Chunk("Value 1")));
list.add(new ListItem(new Chunk("Value 2")));
list.add(new ListItem(new Chunk("Value 3")));
document.add(list);

但它正在产生:

Label:
- Value 1
- Value 2
- Value 3

我该怎么做?

1 个答案:

答案 0 :(得分:1)

List功能不支持您尝试实现的功能,因此您需要使用PdfPTable来解决此问题。如果创建了显示所需结果的ListWithLabel示例:

    PdfPTable table = new PdfPTable(2);
    table.setTotalWidth(200);
    table.setWidths(new int[]{ 1, 10 });
    table.setHorizontalAlignment(Element.ALIGN_LEFT);
    PdfPCell cell;
    cell = new PdfPCell();
    cell.setBorder(PdfPCell.NO_BORDER);
    cell.addElement(new Paragraph("Label"));
    table.addCell(cell);
    cell = new PdfPCell();
    cell.setBorder(PdfPCell.NO_BORDER);
    List list = new List(List.UNORDERED);
    list.add(new ListItem(new Chunk("Value 1")));
    list.add(new ListItem(new Chunk("Value 2")));
    list.add(new ListItem(new Chunk("Value 3")));
    cell.addElement(list);
    table.addCell(cell);