A list is an effective way to organize information and present it clearly and logically. The elements in the list stand out from other parts of the text, encouraging readers to pay attention to them. Depending on your requirements, you can add numbered lists, bulleted lists or multi-level lists to your Word documents. This article demonstrates how to create these types of lists in a Word document in Java using Spire.Doc for Java.
- Insert a Numbered List in Word in Java
- Insert a Bulleted List in Word in Java
- Insert a Multi-Level Numbered List in Word in Java
- Insert a Multi-Level Mixed-Type List in Word in Java
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<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</artifactId> <version>12.11.0</version> </dependency> </dependencies>
Insert a Numbered List in Word in Java
Spire.Doc for Java offers the ListStyle class that you can use to create a numbered list style or a bulleted style. Then, the list style can be applied to a paragraph using Paragraph.getListFormat().applyStyle() method. The steps to create a numbered list are as follows.
- Create a Document object.
- Add a section using Document.addSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list using ListStyle.getLevels().get(index) method, and set the numbering type using ListLevel.setPatternType() method.
- Add the list style to the document using Document.getListStyles().add() method.
- Add several paragraphs to the document using Section.addParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.getListFormat().applyStyle() method.
- Specify the list level using Paragraph.getListFormat().setListLevelNumber() method.
- Save the document to a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListPatternType; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class InsertNumberedList { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Create a numbered list style ListStyle listStyle = new ListStyle(document, ListType.Numbered); listStyle.setName("numberedList"); listStyle.getLevels().get(0).setPatternType(ListPatternType.Decimal_Half_Width); listStyle.getLevels().get(0).setTextPosition(20); document.getListStyles().add(listStyle); //Add a paragraph Paragraph paragraph = section.addParagraph(); paragraph.appendText("Required Web Development Skills:"); paragraph.getFormat().setAfterSpacing(5); //Add a paragraph and apply the numbered list style to it paragraph = section.addParagraph(); paragraph.appendText("HTML"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); //Add another four paragraphs and apply the numbered list style to them paragraph = section.addParagraph(); paragraph.appendText("CSS"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("JavaScript"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("Python"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("MySQL"); paragraph.getListFormat().applyStyle("numberedList"); paragraph.getListFormat().setListLevelNumber(0); //Save the document to file document.saveToFile("output/NumberedList.docx", FileFormat.Docx); } }
Insert a Bulleted List in Word in Java
The process of creating a bulleted list is similar to that of creating a numbered list. The difference is that when creating a list style, you must specify the list type as Bulleted and set a bullet symbol for it. The following are the detailed steps.
- Create a Document object.
- Add a section using Document.addSection() method.
- Create an instance of ListStyle class, specifying the list type to Bulleted.
- Get a specific level of the list using ListStyle.getLevels().get(index) method, and set the bullet symbol using ListLevel.setBulletCharacter() method.
- Add the list style to the document using Document.getListStyles().add() method.
- Add several paragraphs to the document using Section.addParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.getListFormat().applyStyle() method.
- Specify the list level using Paragraph.getListFormat().setListLevelNumber() method.
- Save the document to a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class InsertBulletedList { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Create a bulleted list style ListStyle listStyle = new ListStyle(document, ListType.Bulleted); listStyle.setName("bulletedList"); listStyle.getLevels().get(0).setBulletCharacter("\u00B7"); listStyle.getLevels().get(0).getCharacterFormat().setFontName("Symbol"); listStyle.getLevels().get(0).setTextPosition(20); document.getListStyles().add(listStyle); //Add a paragraph Paragraph paragraph = section.addParagraph(); paragraph.appendText("Computer Science Subjects:"); paragraph.getFormat().setAfterSpacing(5); //Add a paragraph and apply the bulleted list style to it paragraph = section.addParagraph(); paragraph.appendText("Data Structure"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); //Add another five paragraphs and apply the bulleted list style to them paragraph = section.addParagraph(); paragraph.appendText("Algorithm"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("Computer Networks"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("Operating System"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("C Programming"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("Theory of Computations"); paragraph.getListFormat().applyStyle("bulletedList"); paragraph.getListFormat().setListLevelNumber(0); //Save the document to file document.saveToFile("output/BulletedList.docx", FileFormat.Docx); } }
Insert a Multi-Level Numbered List in Word in Java
A multi-level list consists of at least two different levels. Each level of a nested list can be accessed using ListStyle.getLevels().get(index) method. Through ListLevel object, you can set the numbering type and prefix for a certain level. The following are the steps to create a multi-level numbered list in Word.
- Create a Document object.
- Add a section using Document.addSection() method.
- Create an instance of ListStyle class, specifying the list type to Numbered.
- Get a specific level of the list using ListStyle.getLevels().get(index) method, and set the numbering type and prefix.
- Add the list style to the document using Document.getListStyles().add() method.
- Add several paragraphs to the document using Section.addParagraph() method.
- Apply the list style to a specific paragraph using Paragraph.getListFormat().applyStyle() method.
- Specify the list level using Paragraph.getListFormat().setListLevelNumber() method.
- Save the document to a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListPatternType; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class InsertMultilevelNumberedList { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Create a numbered list style, specifying number prefix and pattern type of each level ListStyle listStyle = new ListStyle(document, ListType.Numbered); listStyle.setName("nestedStyle"); listStyle.getLevels().get(0).setPatternType(ListPatternType.Arabic); listStyle.getLevels().get(0).setTextPosition(20); 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); document.getListStyles().add(listStyle); //Add a paragraph Paragraph paragraph = section.addParagraph(); paragraph.appendText("Here's a Multi-Level Numbered List:"); paragraph.getFormat().setAfterSpacing(5f); //Add a paragraph and apply the numbered list style to it paragraph = section.addParagraph(); paragraph.appendText("The first item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(0); //Add another five paragraphs and apply the numbered list stype to them paragraph = section.addParagraph(); paragraph.appendText("The second item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(0); paragraph = section.addParagraph(); paragraph.appendText("The first sub-item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("The second sub-item"); paragraph.getListFormat().continueListNumbering(); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph = section.addParagraph(); paragraph.appendText("A sub-sub-item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("The third item"); paragraph.getListFormat().applyStyle("nestedStyle"); paragraph.getListFormat().setListLevelNumber(0); //Save the document to file document.saveToFile("output/MultilevelNumberedList.docx", FileFormat.Docx); } }
Insert a Multi-Level Mixed-Type List in Word in Java
In some cases, you may want to mix number and symbol in a multi-level list. To create a mixed-type list, you just need to create a numbered list style and a bulleted list style and apply them to different paragraphs. The detailed steps are as follows.
- Create a Document object.
- Add a section using Document.addSection() method.
- Create a numbered list style and a bulleted list style.
- Add several paragraphs to the document using Section.addParagraph() method.
- Apply different list style to different paragraphs using Paragraph.getListFormat().applyStyle() method.
- Save the document to a Word file using Document.saveToFile() method.
- Java
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.documents.ListPatternType; import com.spire.doc.documents.ListStyle; import com.spire.doc.documents.ListType; import com.spire.doc.documents.Paragraph; public class InsertMultilevelMixedTypeList { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Create a numbered list style ListStyle numberedListStyle = new ListStyle(document, ListType.Numbered); numberedListStyle.setName("numberedStyle"); numberedListStyle.getLevels().get(0).setPatternType(ListPatternType.Arabic); numberedListStyle.getLevels().get(0).setTextPosition(20); numberedListStyle.getLevels().get(1).setPatternType(ListPatternType.Low_Letter); document.getListStyles().add(numberedListStyle); //Create a bulleted list style ListStyle bulletedListStyle = new ListStyle(document, ListType.Bulleted); bulletedListStyle.setName("bulletedStyle"); bulletedListStyle.getLevels().get(2).setBulletCharacter("\u002A"); bulletedListStyle.getLevels().get(2).getCharacterFormat().setFontName("Symbol"); document.getListStyles().add(bulletedListStyle); //Add a paragraph Paragraph paragraph = section.addParagraph(); paragraph.appendText("Here's a Multi-Level Mixed List:"); paragraph.getFormat().setAfterSpacing(5f); //Add a paragraph and apply the numbered list style to it paragraph = section.addParagraph(); paragraph.appendText("The first item"); paragraph.getListFormat().applyStyle("numberedStyle"); paragraph.getListFormat().setListLevelNumber(0); //Add another five paragraphs and apply different list stype to them paragraph = section.addParagraph(); paragraph.appendText("The first sub-item"); paragraph.getListFormat().applyStyle("numberedStyle"); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("The second sub-item"); paragraph.getListFormat().setListLevelNumber(1); paragraph.getListFormat().applyStyle("numberedStyle"); paragraph = section.addParagraph(); paragraph.appendText("The first sub-sub-item"); paragraph.getListFormat().applyStyle("bulletedStyle"); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("The second sub-sub-item"); paragraph.getListFormat().applyStyle("bulletedStyle"); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("The second item"); paragraph.getListFormat().applyStyle("numberedStyle"); paragraph.getListFormat().setListLevelNumber(0); //Save the document to file document.saveToFile("output/MultilevelMixedList.docx", FileFormat.Docx); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.