Create Bulleted and Numbered Lists in Word in Java

There are two kinds of lists in Microsoft Word: bulleted list and numbered list. Spire.Doc for Java offers ListFormat.applyBulletStyle method and ListFormat.applyNumberedStyle method to create bulleted lists and numbered lists in Word document.

The following example shows how to create a bulleted list and a numbered list in a Word document using Spire.Doc for Java.

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class Bullets {
    public static void main(String[] args){
        //load the Word document
        Document document = new Document();
        //add a section
        Section section = document.addSection();

        //add 8 paragraphs
        Paragraph paragraph1 = section.addParagraph();
        paragraph1.appendText("Bulleted List");
        paragraph1.applyStyle(BuiltinStyle.Heading_1);
        Paragraph paragraph2 = section.addParagraph();
        paragraph2.appendText("Chapter 1");
        Paragraph paragraph3 = section.addParagraph();
        paragraph3.appendText("Chapter 2");
        Paragraph paragraph4 = section.addParagraph();
        paragraph4.appendText("Chapter 3");
        Paragraph paragraph5 = section.addParagraph();
        paragraph5.appendText("Numbered List");
        paragraph5.applyStyle(BuiltinStyle.Heading_1);
        Paragraph paragraph6 = section.addParagraph();
        paragraph6.appendText("Chapter 1");
        Paragraph paragraph7 = section.addParagraph();
        paragraph7.appendText("Chapter 2");
        Paragraph paragraph8 = section.addParagraph();
        paragraph8.appendText("Chapter 3");

        //create bulleted list for the 2-4 paragraphs
        for(int i = 1; i < 4; i++){
            Paragraph para = section.getParagraphs().get(i);
            para.getListFormat().applyBulletStyle();
            para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
        }

        //create numbered list for the 6-8 paragraphs
        for(int i = 5; i < 8; i++){
            Paragraph para = section.getParagraphs().get(i);
            para.getListFormat().applyNumberedStyle();
            para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
        }

        //save the document
        document.saveToFile("CreateLists.docx", FileFormat.Docx_2013);
    }
}

Output:

Create Bulleted and Numbered Lists in Word in Java