Insert Multi-level Lists in Word Using Java

In Word document, multi-level lists can make the content more organized. Now, this article will give a simple example of how to insert a multi-level list in a Word document using Free Spire.Doc for Java.

Installation
Method 1: Download the free library and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Sample Code

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;

public class CreateMultiLevelList {
public static void main(String[] args) {

//Create a Document object
Document document = new Document();

//Add a section
Section section = document.addSection();

//Create a ListStyle object
ListStyle listStyle = new ListStyle(document, ListType.Numbered);
listStyle.setName("CustomStyle");

//Set the list pattern type and number prefix of each level
listStyle.getLevels().get(0).setPatternType(ListPatternType.Arabic);
listStyle.getLevels().get(1).setNumberPrefix("\u0000.");
listStyle.getLevels().get(1).setPatternType(ListPatternType.Arabic);
listStyle.getLevels().get(2).setNumberPrefix("\u0000.\u0001.");
listStyle.getLevels().get(2).setPatternType(ListPatternType.Arabic);

//Add the custom list style to the list styles collection
document.getListStyles().add(listStyle);

//Add first paragraph and apply list style to it
//The default list level is the first level if you don't set a list level number
Paragraph paragraph = section.addParagraph();
paragraph.appendText("The first item");
paragraph.applyStyle(BuiltinStyle.Heading_1);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add second paragraph and apply list style to it
paragraph = section.addParagraph();
paragraph.appendText("The second item");
paragraph.applyStyle(BuiltinStyle.Heading_1);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add third paragraph, apply list style and set the list level number
paragraph = section.addParagraph();
paragraph.appendText("The first sub-item");
paragraph.applyStyle(BuiltinStyle.Heading_2);
paragraph.getListFormat().setListLevelNumber(1);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add third paragraph, apply list style and set the list level number
paragraph = section.addParagraph();
paragraph.appendText("The second sub-item");
paragraph.applyStyle(BuiltinStyle.Heading_2);
paragraph.getListFormat().continueListNumbering();
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add forth paragraph, apply list style and set the list level number
paragraph = section.addParagraph();
paragraph.appendText("A sub-sub-item");
paragraph.applyStyle(BuiltinStyle.Heading_5);
paragraph.getListFormat().setListLevelNumber(2);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Add fifth paragraph and apply list style to it
paragraph = section.addParagraph();
paragraph.appendText("The third item");
paragraph.applyStyle(BuiltinStyle.Heading_1);
paragraph.getListFormat().applyStyle(listStyle.getName());

//Save the document
document.saveToFile("MultiLevelList.docx", FileFormat.Docx);
}
}

Sharing Java Code