Insert Multi-level Lists in Word in Java

This article demonstrates how to insert a multi-level list in a Word document using Spire.Doc for Java.

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

Insert Multi-level Lists in Word in Java