Free Spire.Doc for Java (免费版)
IntelliJ IDEA
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); }}
分页符、分节符插入效果: