Java 在Word中插入分页符、分节符

2025-10-25 23:32:26

1、Step 1: 通过官网下载jar包,并解压;

Step 2: 在程序中新建一个directory目录,并命名(本示例中命名为lib);

Step 3: 将控件包lib文件夹下的jar文件(如下图)复制到程序中新建的目录下,鼠标右键点击jar文件,选择”Add as Library”—“OK",完成导入。

Java 在Word中插入分页符、分节符

Java 在Word中插入分页符、分节符

1、import com.spire.doc.*;

import com.spire.doc.documents.BreakType;

import com.spire.doc.documents.SectionBreakType;

public class AddPagebreakAndSectionbreak {

    public static void main(String[] args){

        //加载测试文档

        Document doc = new Document("test.docx");

        Section sec= doc.getSections().get(0);

        //插入分页符

        sec.getParagraphs().get(3).appendBreak(BreakType.Page_Break);

        //插入分节符(4种情况)

        sec.getParagraphs().get(8).insertSectionBreak(SectionBreakType.No_Break);//新节与前文内容连续,不从新一页开始

        //sec.getParagraphs().get(8).insertSectionBreak(SectionBreakType.New_Page);//新节从新一页开始

        //sec.getParagraphs().get(8).insertSectionBreak(SectionBreakType.Even_Page);//新节从偶数页开始

        //sec.getParagraphs().get(8).insertSectionBreak(SectionBreakType.Oddpage);//新节从奇数页开始

        //保存文档

        doc.saveToFile("result.docx",FileFormat.Docx_2010);

    }

}

2、分页符、分节符插入效果:

Java 在Word中插入分页符、分节符

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢