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

2025-04-08 19:41:10

以下经验内容将分享通过Java编程在Word中插入分页符、分节符的方法。通过分页符或分节符可合理布局文档每一页内容,同时也便于文档内容的编排和操作。

工具/原料

Free Spire.Doc for Java (免费版)

IntelliJ IDEA

jar文件导入

1、Step 1: 通过官网下载jar包,并解压;Step 2: 在程序中新秃赈沙嚣建一个directory目录,并命名(本示例中命名为lib);Step 3: 瓴烊椹舟将控件包lib文件夹下的jar文件(如下图)复制到程序中新建的目录下,鼠标右键点击jar文件,选择”Add as Library”—“OK",完成导入。

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

Java代码示例

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。
猜你喜欢