com.aspose.words

Class ListCollection

  • java.lang.Object
    • com.aspose.words.ListCollection
  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Iterable
    public class ListCollection 
    extends java.lang.Object

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

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.Lists 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.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.

Example:

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

Example:

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

Example:

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 ("�") before each paragraph.
// Deeper levels of this list will use different symbols, such as "�" and "?".
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");
See Also:
List, ListLevel, ListFormat

Property Getters/Setters Summary
intgetCount()
Gets the count of numbered and bulleted lists in the document.
DocumentBasegetDocument()
Gets the owner document.
Listget(int index)
Gets a list by index.
 
Method Summary
Listadd(Style listStyle)
Creates a new list that references a list style and adds it to the collection of lists in the document.
Listadd(int listTemplate)
Creates a new list based on a predefined template and adds it to the collection of lists in the document.
ListaddCopy(List srcList)
Creates a new list by copying the specified list and adding it to the collection of lists in the document.
ListgetListByListId(int listId)
Gets a list by a list identifier.
java.util.Iterator<List>iterator()
Gets the enumerator object that will enumerate lists in the document.
 

    • Property Getters/Setters Detail

      • getCount

        public int getCount()
        
        Gets the count of numbered and bulleted lists in the document.

        Example:

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

        public DocumentBase getDocument()
        
        Gets the owner document.

        Example:

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

        public List get(int index)
        
        Gets a list by index.

        Example:

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

        Example:

        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)));
    • Method Detail

      • add

        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.

        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.

        Parameters:
        listStyle - The list style.
        Returns:
        The newly created list.

        Example:

        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");
      • add

        public List add(int listTemplate)
        Creates a new list based on a predefined template and adds it to the collection of lists in the document.

        Aspose.Words list templates correspond to the 21 list templates available in the Bullets and Numbering dialog box in Microsoft Word 2003.

        All lists created using this method have 9 list levels.

        Parameters:
        listTemplate - A ListTemplate value. The template of the list.
        Returns:
        The newly created list.

        Example:

        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 ("�") before each paragraph.
        // Deeper levels of this list will use different symbols, such as "�" and "?".
        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");

        Example:

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

        Example:

        Shows how to create a list by applying a new list format 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));
        
        List list = doc.getLists().add(ListTemplate.NUMBER_UPPERCASE_LETTER_DOT);
        
        for (Paragraph paragraph : doc.getFirstSection().getBody().getParagraphs()) {
            paragraph.getListFormat().setList(list);
            paragraph.getListFormat().setListLevelNumber(1);
        }
        
        Assert.assertEquals(3, DocumentHelper.getListItemCount(paras));
      • addCopy

        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.

        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.

        Parameters:
        srcList - The source list to copy from.
        Returns:
        The newly created list.

        Example:

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

        Example:

        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();
        }
      • getListByListId

        public List getListByListId(int listId)
        Gets a list by a list identifier.

        You don't normally need to use this method. Most of the time you apply list formatting to paragraphs just by settings the ListFormat.List property of the ListFormat object.

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

        Example:

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

        public java.util.Iterator<List> iterator()
        Gets the enumerator object that will enumerate lists in the document.

        Example:

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