Java如何实现在Word文档特定段落或文本处插入分页符?
- 内容介绍
- 文章标签
- 相关推荐
本文共计817个文字,预计阅读时间需要4分钟。
在Word文档中插入分页符,可以在指定段落落幕后插入,也可以在特定文本位置插入。以下使用Java代码操作上述两种文档分页的需求的详细方法和步骤:
程序环境- 操作系统:Windows或Linux- 编程语言:Java- 开发工具:Eclipse/IntelliJ IDEA等
导入库javaimport com.artofsolving.jodconverter.*;import com.artofsolving.jodconverter.office.*;
方法实现
1. 在指定段落落幕后插入分页符javapublic void insertPageBreakAfterParagraph(Converter converter, String sourcePath, String targetPath, int paragraphIndex) throws Exception { Document document=converter.convert(new File(sourcePath)); document.getParagraphs().get(paragraphIndex).addBreak(BreakType.PAGE); document.save(new File(targetPath));}
2. 在特定文本位置插入分页符javapublic void insertPageBreakAtPosition(Converter converter, String sourcePath, String targetPath, int position) throws Exception { Document document=converter.convert(new File(sourcePath)); for (int i=0; i 使用示例javapublic static void main(String[] args) { try { OfficeManager officeManager=new OfficeManager(); Converter converter=new OpenOfficeConverter(officeManager); insertPageBreakAfterParagraph(converter, source.docx, target.docx, 5); insertPageBreakAtPosition(converter, source.docx, target.docx, 100); } catch (Exception e) { e.printStackTrace(); }} 以上代码将实现指定段落落幕后插入分页符和特定文本位置插入分页符的功能。 在Word插入分页符可以在指定段落后插入,也可以在特定文本位置处插入。本文,将以Java代码来操作以上两种文档分页需求。下面是详细方法及步骤。 在程序中导入jar,如下两种方法: 方法1:手动引入。将Free Spire.Doc for Java下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序: 方法2(推荐使用):通过 Maven 仓库下载。如下配置pom.xml: <repositories>
<repository>
<id>com.e-iceblue</id>
<url>repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
1.在指定段落后插入分页符
Java import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
public class PageBreak1 {
public static void main(String[] args) {
//创建Document类的对象
Document document = new Document();
//加载Word文档
document.loadFromFile("test.docx");
//获取第一节
Section section = document.getSections().get(0);
//获取第一节中的第3个段落
Paragraph paragraph = section.getParagraphs().get(2);
//添加分页符
paragraph.appendBreak(BreakType.Page_Break);
//保存文档
document.saveToFile("output.docx", FileFormat.Docx_2013);
}
}
Java
import com.spire.doc.Break; import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.BreakType; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextSelection; import com.spire.doc.fields.TextRange; public class PageBreak2 { public static void main(String[] args) { //创建Document类的实例 Document document = new Document(); //加载Word文档 document.loadFromFile("test.docx"); //查找指定文本 TextSelection selection = document.findString("“东盟共同体”宣告成立。", true, true); //获取查找的文本范围 TextRange range = selection.getAsOneRange(); //获取文本范围所在的段落 Paragraph paragraph = range.getOwnerParagraph(); //获取文本范围在段落中的位置索引 int index = paragraph.getChildObjects().indexOf(range); //创建分页 Break pageBreak = new Break(document, BreakType.Page_Break); //在查找的文本位置后面插入分页符 paragraph.getChildObjects().insert(index + 1, pageBreak); //保存文档 document.saveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx_2013); } }
—END—
本文共计817个文字,预计阅读时间需要4分钟。
在Word文档中插入分页符,可以在指定段落落幕后插入,也可以在特定文本位置插入。以下使用Java代码操作上述两种文档分页的需求的详细方法和步骤:
程序环境- 操作系统:Windows或Linux- 编程语言:Java- 开发工具:Eclipse/IntelliJ IDEA等
导入库javaimport com.artofsolving.jodconverter.*;import com.artofsolving.jodconverter.office.*;
方法实现
1. 在指定段落落幕后插入分页符javapublic void insertPageBreakAfterParagraph(Converter converter, String sourcePath, String targetPath, int paragraphIndex) throws Exception { Document document=converter.convert(new File(sourcePath)); document.getParagraphs().get(paragraphIndex).addBreak(BreakType.PAGE); document.save(new File(targetPath));}
2. 在特定文本位置插入分页符javapublic void insertPageBreakAtPosition(Converter converter, String sourcePath, String targetPath, int position) throws Exception { Document document=converter.convert(new File(sourcePath)); for (int i=0; i 使用示例javapublic static void main(String[] args) { try { OfficeManager officeManager=new OfficeManager(); Converter converter=new OpenOfficeConverter(officeManager); insertPageBreakAfterParagraph(converter, source.docx, target.docx, 5); insertPageBreakAtPosition(converter, source.docx, target.docx, 100); } catch (Exception e) { e.printStackTrace(); }} 以上代码将实现指定段落落幕后插入分页符和特定文本位置插入分页符的功能。 在Word插入分页符可以在指定段落后插入,也可以在特定文本位置处插入。本文,将以Java代码来操作以上两种文档分页需求。下面是详细方法及步骤。 在程序中导入jar,如下两种方法: 方法1:手动引入。将Free Spire.Doc for Java下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序: 方法2(推荐使用):通过 Maven 仓库下载。如下配置pom.xml: <repositories>
<repository>
<id>com.e-iceblue</id>
<url>repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
1.在指定段落后插入分页符
Java import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
public class PageBreak1 {
public static void main(String[] args) {
//创建Document类的对象
Document document = new Document();
//加载Word文档
document.loadFromFile("test.docx");
//获取第一节
Section section = document.getSections().get(0);
//获取第一节中的第3个段落
Paragraph paragraph = section.getParagraphs().get(2);
//添加分页符
paragraph.appendBreak(BreakType.Page_Break);
//保存文档
document.saveToFile("output.docx", FileFormat.Docx_2013);
}
}
Java
import com.spire.doc.Break; import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.documents.BreakType; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextSelection; import com.spire.doc.fields.TextRange; public class PageBreak2 { public static void main(String[] args) { //创建Document类的实例 Document document = new Document(); //加载Word文档 document.loadFromFile("test.docx"); //查找指定文本 TextSelection selection = document.findString("“东盟共同体”宣告成立。", true, true); //获取查找的文本范围 TextRange range = selection.getAsOneRange(); //获取文本范围所在的段落 Paragraph paragraph = range.getOwnerParagraph(); //获取文本范围在段落中的位置索引 int index = paragraph.getChildObjects().indexOf(range); //创建分页 Break pageBreak = new Break(document, BreakType.Page_Break); //在查找的文本位置后面插入分页符 paragraph.getChildObjects().insert(index + 1, pageBreak); //保存文档 document.saveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx_2013); } }
—END—

