本文将介绍如何使用Spire.Doc for Java在Word文档中创建简单的项目符号列表和编号列表。
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class BulletedList {
public static void main(String[] args){
//加载Word文档
Document document = new Document();
//添加一个section
Section section = document.addSection();
//添加8个段落
Paragraph paragraph1 = section.addParagraph();
paragraph1.appendText("项目符号列表");
paragraph1.applyStyle(BuiltinStyle.Heading_1);
Paragraph paragraph2 = section.addParagraph();
paragraph2.appendText("第一章");
Paragraph paragraph3 = section.addParagraph();
paragraph3.appendText("第二章");
Paragraph paragraph4 = section.addParagraph();
paragraph4.appendText("第三章");
Paragraph paragraph5 = section.addParagraph();
paragraph5.appendText("编号列表");
paragraph5.applyStyle(BuiltinStyle.Heading_1);
Paragraph paragraph6 = section.addParagraph();
paragraph6.appendText("第一章");
Paragraph paragraph7 = section.addParagraph();
paragraph7.appendText("第二章");
Paragraph paragraph8 = section.addParagraph();
paragraph8.appendText("第三章");
//给第2到第4个段落应用项目符号列表格式
for(int i = 1; i < 4; i++){
Paragraph para = section.getParagraphs().get(i);
para.getListFormat().applyBulletStyle();
para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
}
//给第6到第8个段落应用编号列表格式
for(int i = 5; i < 8; i++){
Paragraph para = section.getParagraphs().get(i);
para.getListFormat().applyNumberedStyle();
para.getListFormat().getCurrentListLevel().setNumberPosition(-10);
}
//保存文档
document.saveToFile("CreateLists.docx", FileFormat.Docx_2013);
}
}
生成文档: