Insert Page Break in Word in Java

·

2 min read

A page break is a special marker that will end the current page and start a new one. In Word, you can insert a page break anywhere you want, and it will not change the formatting of the previous page in your document. This article will share how to insert page breaks into a Word document from the following 2 aspects using Free Spire.Doc for Java library.

  • Insert Page Break after a Specific Paragraph

  • Insert Page Break after a Specific Text

Install the Library

Method 1: Download the free library and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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>

Insert Page Break after a Specific Paragraph

With Free Spire.Doc for Java, you can get a specified paragraph using Section.getParagraphs().get(paragraphIndex) method, and then add a page break to the paragraph using Paragraph.appendBreak(BreakType.Page_Break) method. The complete sample code is shown as below.

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);
    }
}

PageBreakAfterParagraph.jpg

Insert Page Break after a Specific Text

To insert a page break after a specific text, you need to first find the specified text and then get its text range as well as the position index of the text range. Finally, you can insert the page break after the specified text using Paragraph.getChildObjects().insert() method. The complete sample code is shown as below.

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);
    }
}

PageBreakAfterText.jpg