DEV Community

CodeSharing
CodeSharing

Posted on

Create Bulleted and Numbered Lists in Word in Java

Lists in Word documents are often used to make certain content stand out from the regular text. They may attract the readers' attention at first glance because people will naturally notice them more when these lists are displayed in a format differenent from the normal paragraph format. This article will demonstrate how to create simple bulleted and numbered lists in a Word document using Free Spire.Doc for Java.

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

Method 2: You can also 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>
Enter fullscreen mode Exit fullscreen mode

Code Snippet

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

public class Bullets {
    public static void main(String[] args){
        //Create a 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("O. Henry");
        Paragraph paragraph3 = section.addParagraph();
        paragraph3.appendText("Ernest Miller Hemingway");
        Paragraph paragraph4 = section.addParagraph();
        paragraph4.appendText("Ralph Waldo Emerson");
        Paragraph paragraph5 = section.addParagraph();
        paragraph5.appendText("Numbered List");
        paragraph5.applyStyle(BuiltinStyle.Heading_1);
        Paragraph paragraph6 = section.addParagraph();
        paragraph6.appendText("Monday");
        Paragraph paragraph7 = section.addParagraph();
        paragraph7.appendText("Tuesday");
        Paragraph paragraph8 = section.addParagraph();
        paragraph8.appendText("Wednesday");

        //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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Top comments (0)