Monday, 13 September 2021

Create Bulleted Lists, Numbered Lists and Multilevel Lists in Word

In the previous article, I introduced how to create a bulleted list, a numbered list and a multilevel list in PDF using Java codes with the help of a free third-party library called Free Spire.PDF for Java. Today this tutorial will demonstrate how to do the operations above in Word using another free third-party library called Free Spire.Doc for Java.

DEPENDENCY

It is the same as the steps before that we need to add a Jar file to IDEA. We can download the package of the free library from the link, find Spire.Doc.jar in the “lib” folder and then manually add it to IDEA. Or create a Maven project in IDEA, type the following codes in the pom.xml file and finally clicking the button “import changes”.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://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>

USING THE CODE

Create a bulleted list and a numbered list in Word

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

public class BulletedAndNumberedLists {
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("output/CreateLists.docx", FileFormat.Docx_2013);
}
}

Output


Create a multilevel list in Word

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

public class MultilevelList {
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("output/MultiLevelList.docx", FileFormat.Docx);
}
}

Output



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...