A bulleted list is a list with dots in the front and the same indentation for each item, while a numbered list with numbers in the front. Bulleted lists and numbered lists work much better than lengthy text content because, with a well-organized list, readers can easily get the structure and the point of each item. This article shows how to create lists from existing text in Word documents with Spire.Doc for Java.
Install Spire.Doc for Java
First of all, 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.9.0</version> </dependency> </dependencies>
Create a Bulleted List and a Numbered List from Existing Text in a Word Document
Spire.Doc for Java provides two methods, ListFormat.applyBulletStyle() and ListFormat.applyNumberedStyle(), to create bulleted and numbered lists.
The detailed steps of creating bulleted and numbered lists are as follows.
- Create an object of Document class.
- Load a Word document from file using Document.loadFromFile() method.
- Get the first section using Document.getSections().get() method.
- Loop through the 4th to 6th paragraphs.
- Apply bulleted list style to these paragraphs using ListFormat.applyBulletStyle() method and set their position using ListFormat.getCurrentListLevel().setNumberPosition() method.
- Loop through the 10th to 12th paragraphs.
- Apply numbered list style to these paragraphs using ListFormat.applyNumberedStyle() method and set their position using ListFormat.getCurrentListLevel().setNumberPosition() method.
- Save the document 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.Paragraph; import com.spire.doc.formatting.ListFormat; public class CreateLists { public static void main(String[] args) { //Create an object of Document class Document document = new Document(); //Load a Word document from file document.loadFromFile("C:/Samples/Sample2.docx"); //Get the first section Section section = document.getSections().get(0); //Loop through the 4th to the 6th paragraphs for(int i = 3; i <= 5; i++){ Paragraph para = section.getParagraphs().get(i); ListFormat listFormat = para.getListFormat(); //Apply bulleted list style listFormat.applyBulletStyle(); //Set list position listFormat.getCurrentListLevel().setNumberPosition(-10); } //Loop through the 10th to the 12th paragraphs for(int i = 9; i <= 11; i++){ Paragraph para = section.getParagraphs().get(i); ListFormat listFormat = para.getListFormat(); //Apply numbered list style listFormat.applyNumberedStyle(); //Set list position listFormat.getCurrentListLevel().setNumberPosition(-10); } //Save the document document.saveToFile("CreateLists.docx", FileFormat.Docx_2013); } }
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.