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();
    }
}

项目符号、编号列表添加效果:

 

(本文完)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值