How to create a list without indentation?

Tags: ListListItemlist symbol

Is it possible to create numbered lists without any indentation? Something like:

1 A
1-1 A_A
2 B
2-1 B_B
2-1-1 B_B_B
Posted on StackOverflow on Oct 22, 2015 by Valeriane

In iText 7 we create an ordered list like this:

List list1 = new List(ListNumberingType.DECIMAL);
list1.setItemStartIndex(8);       
 
for (int i = 0; i < 5; i++) {
    list1.add("item");
}
document.add(list1);

We get a result like this:

List without indentation
List without indentation

As you see no extra space is added between the label and the content of a list item by default in iText 7. Your question is: how can we remove this indentation. So at first I will show, how we can receive the list with indentation. That's a matter of adding a single line to your code:

List list2 = new List(ListNumberingType.DECIMAL);
list2.setListSymbolAlignment(ListSymbolAlignment.LEFT);
list2.setItemStartIndex(8);       
for (int i = 0; i < 5; i++) {
    list2.add("item");
}
document.add(list2);

Now the result looks like this:

List with indentation
List with indentation

As you can see, extra space was added after items 8 and 9. So to have list without indentation you should just remove line:

list2.setListSymbolAlignment(ListSymbolAlignment.LEFT);

or change the alignment to RIGHT

list2.setListSymbolAlignment(ListSymbolAlignment.RIGHT);

Also you can change the indentation by using setSymbolIndent(float symbolIndent). But take into account that it will change the indentation for all elements (from 8 till 12).

There is also useful information:

  • Custom numbering: no . after the number.

  • A nested structure: 2, followed by 2_1, and so on.

Changing the list symbol in case of ordered lists can be done using the setPreSymbolText() and setPostSymbolText() methods.

Take a look at this snippet:

List list3 = new List(ListNumberingType.DECIMAL);
list3.setItemStartIndex(8);
list3.setPostSymbolText(" ");
for (int i = 0; i < 5; i++) {
        ListItem mainListItem = new ListItem("item");
        List list =  new List(ListNumberingType.DECIMAL);
        list.setPreSymbolText(String.valueOf(8 + i) + "_");
        list.setPostSymbolText(" ");
        list.add("item 1");
        list.add("item 2");
        mainListItem.add(list);
        list3.add(mainListItem);       
 }

First we remove the dot that is added after each number automatically, we use the getPostSymbolText() method for this:

list3.setPostSymbolText(" ");

Then we nest list inside list3, using ListItem of list3. As we want to get a result that looks like 8_1, 8_2, 9_1, etc., we use the setPreSymbolText() method like this:

list.setPreSymbolText(String.valueOf(8 + i) + "_");

Now the result looks like this:

Using pre- and post-symbol
Using pre- and post-symbol

If you're creating Tagged PDF, it's better to use List because iText will then automatically tag that content as a list (e.g. in the context of accessibility).

Click this link if you want to see how to answer this question in iText 5.