Java 添加Word项目符号、编号列表
文档中的项目符号或编号列表常用于罗列关键文本信息。合理使用项目符号和编号,可以使文档的层次结构更清晰、更有条理,突出重点。下面将分享通过Java编程在Word文档中添加项目符号、编号列表的方法。
使用工具:Free Spire.Doc for Java (免费版)
Jar**文件获取及导入:**
方法**1**:官网下载jar文件包。下载后,解压文件,并将lib文件夹下的Spire.Doc.jar文件导入到java程序。参考如下导入效果:
方法**2**:可通过maven仓库安装导入。
Java 代码示例
import com.spire.doc.*;
import com.spire.doc.documents.ListPatternType;
import com.spire.doc.documents.ListType;
import com.spire.doc.documents.Paragraph;
public class AddList {
public static void main(String[]args){
//加载测试文档
Document doc = new Document();
doc.loadFromFile("test.docx");
//获取第一节
Section sec = doc.getSections().get(0);
//给指定段落设置符号列表
for(int i = 1; i< 5;i++){
Paragraph para = sec.getParagraphs().get(i);
para.getListFormat().applyBulletStyle();
para.getListFormat().getCurrentListLevel().setNumberPosition(-5);
para.getListFormat().getCurrentListLevel().setTextPosition(10);
}
//给指定段落设置编号列表(编号列表样式默认是阿拉伯数字,如需其他样式可自行设置数字样式)
for (int j = 7; j < 10 ; j++){
Paragraph para = sec.getParagraphs().get(j);
para.getListFormat().applyNumberedStyle();
para.getListFormat().getCurrentListLevel().setNumberPosition(-5);
para.getListFormat().getCurrentListLevel().setTextPosition(10);
}
for (int z = 10; z < 13 ; z++) {
Paragraph para = sec.getParagraphs().get(z);
para.getListFormat().applyNumberedStyle();
para.getListFormat().getCurrentListLevel().setNumberPosition(-5);
para.getListFormat().setListLevelNumber(2);//设置列表级别(不设置列表级别时,默认级别为1级列表)
para.getListFormat().getCurrentListLevel().setPatternType(ListPatternType.Low_Roman);//设置编号列表数字样式为小写罗马数字
para.getListFormat().getCurrentListLevel().setTextPosition(40);
}
//保存文档
doc.saveToFile("Addlist.docx",FileFormat.Docx_2013);
doc.dispose();
}
}
项目符号、编号列表添加效果:
(本文完)
还没有评论,来说两句吧...