Java: Insert Page Break into Word Documents

By inserting a page break into your Word document, you can end a page at the place you want and begin a new page at once without hitting the enter key repeatedly. In this article, we will demonstrate how to insert page breaks into a Word document in Java using Spire.Doc for Java library.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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</artifactId>
        <version>12.3.1</version>
    </dependency>
</dependencies>
    

Insert Page Break after a Specific Paragraph

The following are the steps to insert page break after a specific paragraph:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Get the desired section using Document.getSections().get(sectionIndex) method.
  • Get the desired paragraph using Section.getParagraphs().get(paragraphIndex) method.
  • Add a page break to the paragraph using Paragraph.appendBreak(BreakType.Page_Break) method.
  • Save the result document using Document.saveToFile() method.
  • Java
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("Sample.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);
    }
}

Java: Insert Page Break into Word Documents

Insert Page Break after a Specific Text

The following are the steps to insert a page break after a specific text:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Find a specific text using Document.findString() method.
  • Access the text range of the searched text using TextSelection.getAsOneRange() method.
  • Get the paragraph where the text range is located using ParagraphBase.getOwnerParagraph() method.
  • Get the position index of the text range in the paragraph using Paragraph.getChildObjects().indexOf() method.
  • Initialize an instance of Break class to create a page break.
  • Insert the page break after the searched text using Paragraph.getChildObjects().insert() method.
  • Save the result document using Document.saveToFile() method.
  • 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 InsertPageBreakAfterText {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();

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

        //Search a specific text
        TextSelection selection = document.findString("celebration", true, true);
        //Get the text range of the seached 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: Insert Page Break into Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.