简体   繁体   中英

List in ColumnText is loosing data that is present when added to the document directly

I'm using iText 5.1.3. The list in the ColumnText isn't complete as shown by the complete list successfully implemented when it is added to the document. If I uncomment the line:

// list2.add(new ListItem(new Chunk("Level 2 - Item 3")));

they both appear to be the same; However, the list format I want is with the line removed compeletely.

public class TestList
{

    public static void main(String[] args) throws IOException, DocumentException
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("testlist.pdf"));
        document.open();
        List list1 = new List(List.ALPHABETICAL, 20);
        list1.add(new ListItem(new Chunk("Level 1 - Item 1")));
        list1.add(new ListItem(new Chunk("Level 1 - Item 2")));
        list1.add(new ListItem(new Chunk("Level 1 - Item 3")));

        List list2 = new List(List.ORDERED, 20);
        list2.add(new ListItem(new Chunk("Level 2 - Item 1")));
        list2.add(new ListItem(new Chunk("Level 2 - Item 2")));

        List list3 = new List(List.ORDERED, 20);
        list3.add(new ListItem(new Chunk("Level 3 - Item 1")));
        list3.add(new ListItem(new Chunk("Level 3 - Item 2")));
        list3.add(new ListItem(new Chunk("Level 3 - Item 3")));
        list3.add(new ListItem(new Chunk("Level 3 - Item 4")));
        list2.add(list3);

        // list2.add(new ListItem(new Chunk("Level 2 - Item 3")));
        list1.add(list2);

        list1.add(new ListItem(new Chunk("Level 1 - Item 4")));
        document.add(list1);
        document.newPage();

        ColumnText cols = new ColumnText(writer.getDirectContent());
        cols.setSimpleColumn(36, 36, 549, 806);
        cols.addElement(list1);
        cols.go();

        document.close();
    }
}

How can I get this desired behavior in the ColumnText as well. I've tried everything I can think of and am at a loss.

I have no idea why it works, but I was just playing around and if you change the commented out line to list2.add(""); it works as expected. Seems more like a hack to me though. The final code looks like:

public class TestList
{

    public static void main(String[] args) throws IOException, DocumentException
    {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("testlist.pdf"));
        document.open();
        List list1 = new List(List.ALPHABETICAL, 20);
        list1.add(new ListItem(new Chunk("Level 1 - Item 1")));
        list1.add(new ListItem(new Chunk("Level 1 - Item 2")));
        list1.add(new ListItem(new Chunk("Level 1 - Item 3")));

        List list2 = new List(List.ORDERED, 20);
        list2.add(new ListItem(new Chunk("Level 2 - Item 1")));
        list2.add(new ListItem(new Chunk("Level 2 - Item 2")));

        List list3 = new List(List.ORDERED, 20);
        list3.add(new ListItem(new Chunk("Level 3 - Item 1")));
        list3.add(new ListItem(new Chunk("Level 3 - Item 2")));
        list3.add(new ListItem(new Chunk("Level 3 - Item 3")));
        list3.add(new ListItem(new Chunk("Level 3 - Item 4")));
        list2.add(list3);

        list2.add(""); // Changed line.
        list1.add(list2);

        list1.add(new ListItem(new Chunk("Level 1 - Item 4")));
        document.add(list1);
        document.newPage();

        ColumnText cols = new ColumnText(writer.getDirectContent());
        cols.setSimpleColumn(36, 36, 549, 806);
        cols.addElement(list1);
        cols.go();

        document.close();
    }
}

Must have something to do with closing the list at level 2. I've seen this question all over the place and never answered, so I hope someone finds this helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM