ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

在 Java 中的 Word 中插入分页符

2022-10-29 14:11:08  阅读:217  来源: 互联网

标签:特殊 特殊字符 java 免费 下载


分页符是一个特殊的标记,它将结束当前页面并开始一个新页面。在 Word 中,您可以在所需的任何位置插入分页符,并且它不会更改文档中上一页的格式。本文将分享如何使用免费的Spire.Doc for Java库从以下2个方面将分页符插入到Word文档中

  • 在特定段落后插入分页符

  • 在特定文本后插入分页符

安装库

方法一:下载免费库并解压缩。然后将 Spire.Doc.jar 文件作为依赖项添加到 Java 应用程序中。
方法二:通过向pom.xml添加以下配置,直接将jar依赖添加到maven项目中。

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>5.2.0</version>
   </dependency>
</dependencies>

在特定段落后插入分页符

使用 Free Spire.Doc for Java,您可以使用 Section.getParagraphs().get(paragraphIndex) 方法获取指定的段落,然后使用 Paragraph.appendBreak(BreakType.Page_Break) 方法向段落添加分页符。完整的示例代码如下所示。

import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.FileFormat;

public class InsertPageBreakAfterParagraph {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Budget.docx");

        //Get the first section
        Section section = document.getSections().get(0);
        //Get the 2nd paragraph in the section
        Paragraph paragraph = section.getParagraphs().get(1);

        //Append a page break to the paragraph
        paragraph.appendBreak(BreakType.Page_Break);

        //Save the result document
        document.saveToFile("InsertPageBreak.docx", FileFormat.Docx_2013);
    }
}

在特定文本后插入分页符

要在特定文本之后插入分页符,您需要首先找到指定的文本,然后获取其文本范围以及文本范围的位置索引。最后,您可以使用 Paragraph.getChildObjects().insert() 方法在指定文本之后插入分页符。完整的示例代码如下所示。

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 InsertPageBreakAfterText {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();

        //Load a Word document
        document.loadFromFile("Budget.docx");

        //Search a specific text
        TextSelection selection = document.findString("anticipated", true, true);
        //Get the text range of the searched text
        TextRange range = selection.getAsOneRange();
        //Get the paragraph where the text range is located
        Paragraph paragraph = range.getOwnerParagraph();
        //Get the position index of the text range in the paragraph
        int index = paragraph.getChildObjects().indexOf(range);

        //Create a page break
        Break pageBreak = new Break(document, BreakType.Page_Break);
        //Insert the page break after the searched text
        paragraph.getChildObjects().insert(index + 1, pageBreak);

        //Save the result document
        document.saveToFile("InsertPageBreakAfterText.docx", FileFormat.Docx_2013);
    }
}

标签:特殊,特殊字符,java,免费,下载
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有