ListCollection

Inheritance: java.lang.Object

All Implemented Interfaces: java.lang.Cloneable, java.lang.Iterable

public class ListCollection implements Cloneable, Iterable

Stores and manages formatting of bulleted and numbered lists used in a document.

To learn more, visit the Working with Lists documentation article.

Remarks:

A list in a Microsoft Word document is a set of list formatting properties. The formatting of the lists is stored in the ListCollection collection separately from the paragraphs of text.

You do not create objects of this class. There is always only one ListCollection object per document and it is accessible via the DocumentBase.getLists() property.

To create a new list based on a predefined list template or based on a list style, use the add(com.aspose.words.Style) method.

To create a new list with formatting identical to an existing list, use the addCopy(com.aspose.words.List) method.

To make a paragraph bulleted or numbered, you need to apply list formatting to a paragraph by assigning a List object to the ListFormat.getList() / ListFormat.setList(com.aspose.words.List) property of ListFormat.

To remove list formatting from a paragraph, use the ListFormat.removeNumbers() method.

If you know a bit about WordprocessingML, then you might know it defines separate concepts for “list” and “list definition”. This exactly corresponds to how list formatting is stored in a Microsoft Word document at the low level. List definition is like a “schema” and list is like an instance of a list definition.

To simplify programming model, Aspose.Words hides the distinction between list and list definition in much the same way like Microsoft Word hides this in its user interface. This allows you to concentrate more on how you want your document to look like, rather than building low-level objects to satisfy requirements of the Microsoft Word file format.

It is not possible to delete lists once they are created in the current version of Aspose.Words. This is similar to Microsoft Word where user does not have explicit control over list definitions.

Examples:

Shows how to create a document with a sample of all the lists from another document.


 public void printOutAllLists() throws Exception {
     Document srcDoc = new Document(getMyDir() + "Rendering.docx");

     Document dstDoc = new Document();
     DocumentBuilder builder = new DocumentBuilder(dstDoc);

     for (List srcList : srcDoc.getLists()) {
         List dstList = dstDoc.getLists().addCopy(srcList);
         addListSample(builder, dstList);
     }

     dstDoc.save(getArtifactsDir() + "Lists.PrintOutAllLists.docx");
 }

 private static void addListSample(final DocumentBuilder builder, final List list) {
     builder.writeln("Sample formatting of list with ListId:" + list.getListId());
     builder.getListFormat().setList(list);
     for (int i = 0; i < list.getListLevels().getCount(); i++) {
         builder.getListFormat().setListLevelNumber(i);
         builder.writeln("Level " + i);
     }
     builder.getListFormat().removeNumbers();
     builder.writeln();
 }
 

Shows how to restart numbering in a list by copying a list.


 Document doc = new Document();

 // A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents.
 // We can create nested lists by increasing the indent level.
 // We can begin and end a list by using a document builder's "ListFormat" property.
 // Each paragraph that we add between a list's start and the end will become an item in the list.
 // Create a list from a Microsoft Word template, and customize its first list level.
 List list1 = doc.getLists().add(ListTemplate.NUMBER_ARABIC_PARENTHESIS);
 list1.getListLevels().get(0).getFont().setColor(Color.RED);
 list1.getListLevels().get(0).setAlignment(ListLevelAlignment.RIGHT);

 // Apply our list to some paragraphs.
 DocumentBuilder builder = new DocumentBuilder(doc);

 builder.writeln("List 1 starts below:");
 builder.getListFormat().setList(list1);
 builder.writeln("Item 1");
 builder.writeln("Item 2");
 builder.getListFormat().removeNumbers();

 // We can add a copy of an existing list to the document's list collection
 // to create a similar list without making changes to the original.
 List list2 = doc.getLists().addCopy(list1);
 list2.getListLevels().get(0).getFont().setColor(Color.BLUE);
 list2.getListLevels().get(0).setStartAt(10);

 // Apply the second list to new paragraphs.
 builder.writeln("List 2 starts below:");
 builder.getListFormat().setList(list2);
 builder.writeln("Item 1");
 builder.writeln("Item 2");
 builder.getListFormat().removeNumbers();

 doc.save(getArtifactsDir() + "Lists.RestartNumberingUsingListCopy.docx");
 

Shows how to work with list levels.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 Assert.assertFalse(builder.getListFormat().isListItem());

 // A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents.
 // We can create nested lists by increasing the indent level.
 // We can begin and end a list by using a document builder's "ListFormat" property.
 // Each paragraph that we add between a list's start and the end will become an item in the list.
 // Below are two types of lists that we can create using a document builder.
 // 1 -  A numbered list:
 // Numbered lists create a logical order for their paragraphs by numbering each item.
 builder.getListFormat().setList(doc.getLists().add(ListTemplate.NUMBER_DEFAULT));

 Assert.assertTrue(builder.getListFormat().isListItem());

 // By setting the "ListLevelNumber" property, we can increase the list level
 // to begin a self-contained sub-list at the current list item.
 // The Microsoft Word list template called "NumberDefault" uses numbers to create list levels for the first list level.
 // Deeper list levels use letters and lowercase Roman numerals.
 for (int i = 0; i < 9; i++) {
     builder.getListFormat().setListLevelNumber(i);
     builder.writeln("Level " + i);
 }

 // 2 -  A bulleted list:
 // This list will apply an indent and a bullet symbol ("\u2022") before each paragraph.
 // Deeper levels of this list will use different symbols, such as "\u25a0" and "\u25cb".
 builder.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DEFAULT));

 for (int i = 0; i < 9; i++) {
     builder.getListFormat().setListLevelNumber(i);
     builder.writeln("Level " + i);
 }

 // We can disable list formatting to not format any subsequent paragraphs as lists by un-setting the "List" flag.
 builder.getListFormat().setList(null);

 Assert.assertFalse(builder.getListFormat().isListItem());

 doc.save(getArtifactsDir() + "Lists.SpecifyListLevel.docx");
 

Methods

MethodDescription
add(Style listStyle)Creates a new list that references a list style and adds it to the collection of lists in the document.
add(int listTemplate)
addCopy(List srcList)Creates a new list by copying the specified list and adding it to the collection of lists in the document.
get(int index)Gets a list by index.
getCount()Gets the count of numbered and bulleted lists in the document.
getDocument()Gets the owner document.
getListByListId(int listId)Gets a list by a list identifier.
iterator()Gets the enumerator object that will enumerate lists in the document.

add(Style listStyle)

public List add(Style listStyle)

Creates a new list that references a list style and adds it to the collection of lists in the document.

Remarks:

The newly created list references the list style. If you change the properties of the list style, it is reflected in the properties of the list. Vice versa, if you change the properties of the list, it is reflected in the properties of the list style.

Examples:

Shows how to create a list style and use it in a document.


 Document doc = new Document();

 // A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents.
 // We can create nested lists by increasing the indent level.
 // We can begin and end a list by using a document builder's "ListFormat" property.
 // Each paragraph that we add between a list's start and the end will become an item in the list.
 // We can contain an entire List object within a style.
 Style listStyle = doc.getStyles().add(StyleType.LIST, "MyListStyle");

 List list1 = listStyle.getList();

 Assert.assertTrue(list1.isListStyleDefinition());
 Assert.assertFalse(list1.isListStyleReference());
 Assert.assertTrue(list1.isMultiLevel());
 Assert.assertEquals(listStyle, list1.getStyle());

 // Change the appearance of all list levels in our list.
 for (ListLevel level : list1.getListLevels()) {
     level.getFont().setName("Verdana");
     level.getFont().setColor(Color.BLUE);
     level.getFont().setBold(true);
 }

 DocumentBuilder builder = new DocumentBuilder(doc);

 builder.writeln("Using list style first time:");

 // Create another list from a list within a style.
 List list2 = doc.getLists().add(listStyle);

 Assert.assertFalse(list2.isListStyleDefinition());
 Assert.assertTrue(list2.isListStyleReference());
 Assert.assertEquals(listStyle, list2.getStyle());

 // Add some list items that our list will format.
 builder.getListFormat().setList(list2);
 builder.writeln("Item 1");
 builder.writeln("Item 2");
 builder.getListFormat().removeNumbers();

 builder.writeln("Using list style second time:");

 // Create and apply another list based on the list style.
 List list3 = doc.getLists().add(listStyle);
 builder.getListFormat().setList(list3);
 builder.writeln("Item 1");
 builder.writeln("Item 2");
 builder.getListFormat().removeNumbers();

 builder.getDocument().save(getArtifactsDir() + "Lists.CreateAndUseListStyle.docx");
 

Parameters:

ParameterTypeDescription
listStyleStyleThe list style.

Returns: List - The newly created list.

add(int listTemplate)

public List add(int listTemplate)

Parameters:

ParameterTypeDescription
listTemplateint

Returns: List

addCopy(List srcList)

public List addCopy(List srcList)

Creates a new list by copying the specified list and adding it to the collection of lists in the document.

Remarks:

The source list can be from any document. If the source list belongs to a different document, a copy of the list is created and added to the current document.

If the source list is a reference to or a definition of a list style, the newly created list is not related to the original list style.

Examples:

Shows how to create a document with a sample of all the lists from another document.


 public void printOutAllLists() throws Exception {
     Document srcDoc = new Document(getMyDir() + "Rendering.docx");

     Document dstDoc = new Document();
     DocumentBuilder builder = new DocumentBuilder(dstDoc);

     for (List srcList : srcDoc.getLists()) {
         List dstList = dstDoc.getLists().addCopy(srcList);
         addListSample(builder, dstList);
     }

     dstDoc.save(getArtifactsDir() + "Lists.PrintOutAllLists.docx");
 }

 private static void addListSample(final DocumentBuilder builder, final List list) {
     builder.writeln("Sample formatting of list with ListId:" + list.getListId());
     builder.getListFormat().setList(list);
     for (int i = 0; i < list.getListLevels().getCount(); i++) {
         builder.getListFormat().setListLevelNumber(i);
         builder.writeln("Level " + i);
     }
     builder.getListFormat().removeNumbers();
     builder.writeln();
 }
 

Shows how to restart numbering in a list by copying a list.


 Document doc = new Document();

 // A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents.
 // We can create nested lists by increasing the indent level.
 // We can begin and end a list by using a document builder's "ListFormat" property.
 // Each paragraph that we add between a list's start and the end will become an item in the list.
 // Create a list from a Microsoft Word template, and customize its first list level.
 List list1 = doc.getLists().add(ListTemplate.NUMBER_ARABIC_PARENTHESIS);
 list1.getListLevels().get(0).getFont().setColor(Color.RED);
 list1.getListLevels().get(0).setAlignment(ListLevelAlignment.RIGHT);

 // Apply our list to some paragraphs.
 DocumentBuilder builder = new DocumentBuilder(doc);

 builder.writeln("List 1 starts below:");
 builder.getListFormat().setList(list1);
 builder.writeln("Item 1");
 builder.writeln("Item 2");
 builder.getListFormat().removeNumbers();

 // We can add a copy of an existing list to the document's list collection
 // to create a similar list without making changes to the original.
 List list2 = doc.getLists().addCopy(list1);
 list2.getListLevels().get(0).getFont().setColor(Color.BLUE);
 list2.getListLevels().get(0).setStartAt(10);

 // Apply the second list to new paragraphs.
 builder.writeln("List 2 starts below:");
 builder.getListFormat().setList(list2);
 builder.writeln("Item 1");
 builder.writeln("Item 2");
 builder.getListFormat().removeNumbers();

 doc.save(getArtifactsDir() + "Lists.RestartNumberingUsingListCopy.docx");
 

Parameters:

ParameterTypeDescription
srcListListThe source list to copy from.

Returns: List - The newly created list.

get(int index)

public List get(int index)

Gets a list by index.

Examples:

Shows how to verify owner document properties of lists.


 Document doc = new Document();

 ListCollection lists = doc.getLists();

 Assert.assertEquals(doc, lists.getDocument());

 List list = lists.add(ListTemplate.BULLET_DEFAULT);

 Assert.assertEquals(doc, list.getDocument());

 System.out.println("Current list count: " + lists.getCount());
 System.out.println("Is the first document list: " + (lists.get(0).equals(list)));
 System.out.println("ListId: " + list.getListId());
 System.out.println("List is the same by ListId: " + (lists.getListByListId(1).equals(list)));
 

Shows how to apply list formatting of an existing list to a collection of paragraphs.


 Document doc = new Document();
 DocumentBuilder builder = new DocumentBuilder(doc);

 builder.writeln("Paragraph 1");
 builder.writeln("Paragraph 2");
 builder.write("Paragraph 3");

 NodeCollection paras = doc.getChildNodes(NodeType.PARAGRAPH, true);

 Assert.assertEquals(0, DocumentHelper.getListItemCount(paras));

 doc.getLists().add(ListTemplate.NUMBER_DEFAULT);
 List list = doc.getLists().get(0);

 for (Paragraph paragraph : doc.getFirstSection().getBody().getParagraphs()) {
     paragraph.getListFormat().setList(list);
     paragraph.getListFormat().setListLevelNumber(2);
 }

 paras = doc.getChildNodes(NodeType.PARAGRAPH, true);

 Assert.assertEquals(3, DocumentHelper.getListItemCount(paras));
 

Parameters:

ParameterTypeDescription
indexint

Returns: List - A list by index.

getCount()

public int getCount()

Gets the count of numbered and bulleted lists in the document.

Examples:

Shows how to verify owner document properties of lists.


 Document doc = new Document();

 ListCollection lists = doc.getLists();

 Assert.assertEquals(doc, lists.getDocument());

 List list = lists.add(ListTemplate.BULLET_DEFAULT);

 Assert.assertEquals(doc, list.getDocument());

 System.out.println("Current list count: " + lists.getCount());
 System.out.println("Is the first document list: " + (lists.get(0).equals(list)));
 System.out.println("ListId: " + list.getListId());
 System.out.println("List is the same by ListId: " + (lists.getListByListId(1).equals(list)));
 

Returns: int - The count of numbered and bulleted lists in the document.

getDocument()

public DocumentBase getDocument()

Gets the owner document.

Examples:

Shows how to verify owner document properties of lists.


 Document doc = new Document();

 ListCollection lists = doc.getLists();

 Assert.assertEquals(doc, lists.getDocument());

 List list = lists.add(ListTemplate.BULLET_DEFAULT);

 Assert.assertEquals(doc, list.getDocument());

 System.out.println("Current list count: " + lists.getCount());
 System.out.println("Is the first document list: " + (lists.get(0).equals(list)));
 System.out.println("ListId: " + list.getListId());
 System.out.println("List is the same by ListId: " + (lists.getListByListId(1).equals(list)));
 

Returns: DocumentBase - The owner document.

getListByListId(int listId)

public List getListByListId(int listId)

Gets a list by a list identifier.

Remarks:

You don’t normally need to use this method. Most of the time you apply list formatting to paragraphs just by settings the ListFormat.getList() / ListFormat.setList(com.aspose.words.List) property of the ListFormat object.

Examples:

Shows how to verify owner document properties of lists.


 Document doc = new Document();

 ListCollection lists = doc.getLists();

 Assert.assertEquals(doc, lists.getDocument());

 List list = lists.add(ListTemplate.BULLET_DEFAULT);

 Assert.assertEquals(doc, list.getDocument());

 System.out.println("Current list count: " + lists.getCount());
 System.out.println("Is the first document list: " + (lists.get(0).equals(list)));
 System.out.println("ListId: " + list.getListId());
 System.out.println("List is the same by ListId: " + (lists.getListByListId(1).equals(list)));
 

Parameters:

ParameterTypeDescription
listIdintThe list identifier.

Returns: List - Returns the list object. Returns null if a list with the specified identifier was not found.

iterator()

public Iterator iterator()

Gets the enumerator object that will enumerate lists in the document.

Examples:

Shows how to create a document with a sample of all the lists from another document.


 public void printOutAllLists() throws Exception {
     Document srcDoc = new Document(getMyDir() + "Rendering.docx");

     Document dstDoc = new Document();
     DocumentBuilder builder = new DocumentBuilder(dstDoc);

     for (List srcList : srcDoc.getLists()) {
         List dstList = dstDoc.getLists().addCopy(srcList);
         addListSample(builder, dstList);
     }

     dstDoc.save(getArtifactsDir() + "Lists.PrintOutAllLists.docx");
 }

 private static void addListSample(final DocumentBuilder builder, final List list) {
     builder.writeln("Sample formatting of list with ListId:" + list.getListId());
     builder.getListFormat().setList(list);
     for (int i = 0; i < list.getListLevels().getCount(); i++) {
         builder.getListFormat().setListLevelNumber(i);
         builder.writeln("Level " + i);
     }
     builder.getListFormat().removeNumbers();
     builder.writeln();
 }
 

Returns: java.util.Iterator