DEV Community

loading...

Create, read and remove bulleted and numbered lists in Word in Java

eiceblue profile image E-iceblue Product Family ・4 min read

Introduction

Microsoft Word provides bullets and numbers to help us put the list of items in our Word document into a nice order. This article illustrates how to add bulleted and numbered lists to a Word document, and then read and remove the bulleted and numbered lists from the Word document within Java application.

The library we need

Free Spire.Doc for Java

Before using the below code, we need to add dependency to Free Spire.Doc for Java library. For maven application, we can add the below code into pom.xml to install Free Spire.Doc for Java (version 2.6.2) from maven repository:

<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>2.6.2</version>
    </dependency>
</dependencies>

We can also download Free Spire.Doc for Java from the official website:
https://www.e-iceblue.com/Download/doc-for-java-free.html

Sample code

Create simple bulleted and numbered lists

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

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

        //Add 8 paragraphs
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("Part 1");
        paragraph.applyStyle(BuiltinStyle.Heading_1);
        paragraph = section.addParagraph();
        paragraph.appendText("Chapter 1");
        paragraph = section.addParagraph();
        paragraph.appendText("Chapter 2");
        paragraph = section.addParagraph();
        paragraph.appendText("Chapter 3");
        paragraph = section.addParagraph();
        paragraph.appendText("Part 2");
        paragraph.applyStyle(BuiltinStyle.Heading_1);
        paragraph = section.addParagraph();
        paragraph.appendText("Chapter 1");
        paragraph = section.addParagraph();
        paragraph.appendText("Chapter 2");
        paragraph = section.addParagraph();
        paragraph.appendText("Chapter 3");

        //apply bullets to 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);
        }

        //apply numbering to 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 resultant document
        document.saveToFile("Output.docx", FileFormat.Docx_2013);
    }
}

Output:

Create multi-level bulleted and numbered lists

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

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

        //add a title paragraph
        Paragraph paragraph = section.addParagraph();
        paragraph.appendText("Lists");
        paragraph.applyStyle(BuiltinStyle.Title);

        //add a paragraph
        paragraph = section.addParagraph();
        paragraph.appendText("Numbered List:").getCharacterFormat().setBold(true);

        //create a numbered list
        ListStyle numberList = new ListStyle(document, ListType.Numbered);
        numberList.setName("numberList");
        numberList.getLevels().get(1).setNumberPrefix("\u0000.");
        numberList.getLevels().get(1).setPatternType(ListPatternType.Arabic);
        numberList.getLevels().get(2).setNumberPrefix("\u0000.\u0001");
        numberList.getLevels().get(2).setPatternType(ListPatternType.Arabic);

        //create a bulleted list
        ListStyle bulletList= new ListStyle(document, ListType.Bulleted);
        bulletList.setName("bulletList");

        //add the numbered and bulleted list into the document
        document.getListStyles().add(numberList);
        document.getListStyles().add(bulletList);

        //add paragraphs and apply the numbered list
        paragraph = section.addParagraph();
        paragraph.appendText("List Item 1");
        paragraph.getListFormat().applyStyle(numberList.getName());

        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2");
        paragraph.getListFormat().applyStyle(numberList.getName());

        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2.1");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(1);

        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2.2");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(1);

        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2.2.1");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(2);
        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2.2.2");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(2);
        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2.2.3");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(2);

        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2.3");
        paragraph.getListFormat().applyStyle(numberList.getName());
        paragraph.getListFormat().setListLevelNumber(1);

        paragraph = section.addParagraph();
        paragraph.appendText("List Item 3");
        paragraph.getListFormat().applyStyle(numberList.getName());

        //add paragraphs and apply the bulleted list
        paragraph = section.addParagraph();
        paragraph.appendText("Bulleted List:").getCharacterFormat().setBold(true);

        paragraph = section.addParagraph();
        paragraph.appendText("List Item 1");
        paragraph.getListFormat().applyStyle(bulletList.getName());
        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2");
        paragraph.getListFormat().applyStyle(bulletList.getName());

        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2.1");
        paragraph.getListFormat().applyStyle(bulletList.getName());
        paragraph.getListFormat().setListLevelNumber(1);
        paragraph = section.addParagraph();
        paragraph.appendText("List Item 2.2");
        paragraph.getListFormat().applyStyle(bulletList.getName());
        paragraph.getListFormat().setListLevelNumber(1);
        paragraph = section.addParagraph();
        paragraph.appendText("List Item 3");
        paragraph.getListFormat().applyStyle(bulletList.getName());

        //save the resultant document
        document.saveToFile("MultiLevelList.docx", FileFormat.Docx);

    }
}

Output:

Read bulleted and numbered lists

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

public class ReadList {
    public static void main(String[] args){
        //load the MultiLevelList.docx document
        Document document = new Document();
        document.loadFromFile("MultiLevelList.docx");

        //get the 1st section
        Section section = document.getSections().get(0);

        //loop through the paragraphs in the section and read lists
        for(int i = 0; i < section.getParagraphs().getCount(); i++){
            Paragraph paragraph = section.getParagraphs().get(i);
            String list = paragraph.getListText();
            System.out.println(list);
        }
    }
}

Output:

Remove bulleted and numbered lists

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

public class RemoveList {
    public static void main(String[] args){
        //load the MultiLevelList.docx document
        Document document = new Document();
        document.loadFromFile("MultiLevelList.docx");

        //get the 1st section
        Section section = document.getSections().get(0);

        //Loop through the paragraphs in the section and remove lists
        for(int i = 0; i < section.getParagraphs().getCount(); i++){
            Paragraph paragraph = section.getParagraphs().get(i);
            paragraph.getListFormat().removeList();
        }

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

Output:

More information:

Product Home: https://www.e-iceblue.com/Introduce/free-doc-for-java.html
Forum: https://www.e-iceblue.com/forum/spire-doc-f6.html?sid=b288c0f77c705ba49870210964622cd8

Discussion (0)

pic
Editor guide