|
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 Document.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 specify list level number when building a list using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a numbered list based on one of the Microsoft Word list templates and
// apply it to the current paragraph in the document builder.
builder.getListFormat().setList(doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT));
// There are 9 levels in this list, lets try them all.
for (int i = 0; i < 9; i++)
{
builder.getListFormat().setListLevelNumber(i);
builder.writeln("Level " + i);
}
// Create a bulleted list based on one of the Microsoft Word list templates
// and apply it to the current paragraph in the document builder.
builder.getListFormat().setList(doc.getLists().add(ListTemplate.BULLET_DIAMONDS));
// There are 9 levels in this list, lets try them all.
for (int i = 0; i < 9; i++)
{
builder.getListFormat().setListLevelNumber(i);
builder.writeln("Level " + i);
}
// This is a way to stop list formatting.
builder.getListFormat().setList(null);
builder.getDocument().save(getMyDir() + "Lists.SpecifyListLevel Out.doc");Example: Enumerates through all lists defined in one document and creates a sample of those lists in another document.
public void PrintOutAllLists() throws Exception
{
// You can use any of your documents to try this little program out.
Document srcDoc = new Document(getMyDir() + "Lists.PrintOutAllLists.doc");
// This will be the sample document we product.
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
for (List srcList : srcDoc.getLists())
{
// This copies the list formatting from the source into the destination document.
List dstList = dstDoc.getLists().addCopy(srcList);
AddListSample(builder, dstList);
}
dstDoc.save(getMyDir() + "Lists.PrintOutAllLists Out.doc");
}
private static void AddListSample(DocumentBuilder builder, List list) throws Exception
{
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();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a list based on a template.
List list1 = doc.getLists().add(ListTemplate.NUMBER_ARABIC_PARENTHESIS);
// Modify the formatting of the list.
list1.getListLevels().get(0).getFont().setColor(Color.RED);
list1.getListLevels().get(0).setAlignment(ListLevelAlignment.RIGHT);
builder.writeln("List 1 starts below:");
// Use the first list in the document for a while.
builder.getListFormat().setList(list1);
builder.writeln("Item 1");
builder.writeln("Item 2");
builder.getListFormat().removeNumbers();
// Now I want to reuse the first list, but need to restart numbering.
// This should be done by creating a copy of the original list formatting.
List list2 = doc.getLists().addCopy(list1);
// We can modify the new list in any way. Including setting new start number.
list2.getListLevels().get(0).setStartAt(10);
// Use the second list in the document.
builder.writeln("List 2 starts below:");
builder.getListFormat().setList(list2);
builder.writeln("Item 1");
builder.writeln("Item 2");
builder.getListFormat().removeNumbers();
builder.getDocument().save(getMyDir() + "Lists.RestartNumberingUsingListCopy Out.doc"); - See Also:
- List, ListLevel, ListFormat
|
Property Getters/Setters Summary |
int | getCount() | | |
Gets the count of numbered and bulleted lists in the document.
|
Document | getDocument() | | |
Gets the owner document.
|
List | get(int index) | | |
Gets a list by index.
|
|
Method Summary |
List | add(Style listStyle) | |
|
Creates a new list that references a list style and adds it to the collection of lists in the document.
|
List | add(int template) | |
|
Creates a new list based on a predefined template and adds it to the collection of lists in the document.
|
List | addCopy(List srcList) | |
|
Creates a new list by copying the specified list and adding it to the collection of lists in the document.
|
List | getListByListId(int listId) | |
|
Gets a list by a list identifier.
|
java.util.Iterator | 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: Illustrates the owner document properties of lists.
Document doc = new Document();
ListCollection lists = doc.getLists();
Assert.assertEquals(doc, lists.getDocument());
Assert.assertEquals(0, lists.getCount());
List list = lists.add(ListTemplate.BULLET_DEFAULT);
Assert.assertEquals(doc, list.getDocument());
Assert.assertEquals(1, lists.getCount());
Assert.assertEquals(list, lists.get(0));
Assert.assertEquals(1, list.getListId());
Assert.assertEquals(list, lists.getListByListId(1));
getDocument | |
public Document getDocument()
|
-
Gets the owner document.
Example: Illustrates the owner document properties of lists.
Document doc = new Document();
ListCollection lists = doc.getLists();
Assert.assertEquals(doc, lists.getDocument());
Assert.assertEquals(0, lists.getCount());
List list = lists.add(ListTemplate.BULLET_DEFAULT);
Assert.assertEquals(doc, list.getDocument());
Assert.assertEquals(1, lists.getCount());
Assert.assertEquals(list, lists.get(0));
Assert.assertEquals(1, list.getListId());
Assert.assertEquals(list, lists.getListByListId(1));
get | |
public List get(int index)
|
-
Gets a list by index.
Example: Illustrates the owner document properties of lists.
Document doc = new Document();
ListCollection lists = doc.getLists();
Assert.assertEquals(doc, lists.getDocument());
Assert.assertEquals(0, lists.getCount());
List list = lists.add(ListTemplate.BULLET_DEFAULT);
Assert.assertEquals(doc, list.getDocument());
Assert.assertEquals(1, lists.getCount());
Assert.assertEquals(list, lists.get(0));
Assert.assertEquals(1, list.getListId());
Assert.assertEquals(list, lists.getListByListId(1)); Example: Applies list formatting of an existing list to a collection of paragraphs.
Body body = doc.getFirstSection().getBody();
List list = doc.getLists().get(0);
for (Paragraph paragraph : body.getParagraphs())
{
paragraph.getListFormat().setList(list);
paragraph.getListFormat().setListLevelNumber(2);
}
iterator | |
public java.util.Iterator iterator() |
-
Gets the enumerator object that will enumerate lists in the document.
Example: Enumerates through all lists defined in one document and creates a sample of those lists in another document.
public void PrintOutAllLists() throws Exception
{
// You can use any of your documents to try this little program out.
Document srcDoc = new Document(getMyDir() + "Lists.PrintOutAllLists.doc");
// This will be the sample document we product.
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
for (List srcList : srcDoc.getLists())
{
// This copies the list formatting from the source into the destination document.
List dstList = dstDoc.getLists().addCopy(srcList);
AddListSample(builder, dstList);
}
dstDoc.save(getMyDir() + "Lists.PrintOutAllLists Out.doc");
}
private static void AddListSample(DocumentBuilder builder, List list) throws Exception
{
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();
}
add | |
public List add(int template)
throws java.lang.Exception |
-
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:
template - A ListTemplate value. The template of the list.
- Returns:
- The newly created list.
Example: Shows how to restart numbering in a list by copying a list.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a list based on a template.
List list1 = doc.getLists().add(ListTemplate.NUMBER_ARABIC_PARENTHESIS);
// Modify the formatting of the list.
list1.getListLevels().get(0).getFont().setColor(Color.RED);
list1.getListLevels().get(0).setAlignment(ListLevelAlignment.RIGHT);
builder.writeln("List 1 starts below:");
// Use the first list in the document for a while.
builder.getListFormat().setList(list1);
builder.writeln("Item 1");
builder.writeln("Item 2");
builder.getListFormat().removeNumbers();
// Now I want to reuse the first list, but need to restart numbering.
// This should be done by creating a copy of the original list formatting.
List list2 = doc.getLists().addCopy(list1);
// We can modify the new list in any way. Including setting new start number.
list2.getListLevels().get(0).setStartAt(10);
// Use the second list in the document.
builder.writeln("List 2 starts below:");
builder.getListFormat().setList(list2);
builder.writeln("Item 1");
builder.writeln("Item 2");
builder.getListFormat().removeNumbers();
builder.getDocument().save(getMyDir() + "Lists.RestartNumberingUsingListCopy Out.doc");Example: Shows how to specify list level number when building a list using DocumentBuilder.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Create a numbered list based on one of the Microsoft Word list templates and
// apply it to the current paragraph in the document builder.
builder.getListFormat().setList(doc.getLists().add(ListTemplate.NUMBER_ARABIC_DOT));
// There are 9 levels in this list, lets try them all.
for (int i = 0; i < 9; i++)
{
builder.getListFormat().setListLevelNumber(i);
builder.writeln("Level " + i);
}
// Create a bulleted list based on one of the Microsoft Word list templates
// a
|