Insert Page Break and Section Break in Word Document in Java

We can use page break to split a page into two pages and use section break to start a new section. This article demonstrates how to insert page break and section break into a Word document using Spire.Doc for Java.

Insert page break

The following code example shows how to insert page break to a Word document by using the appendBreak method in Paragraph class.

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class SectionBreakAndPageBreak {
    public static void main(String[] args){
        //load Word document
        Document document = new Document();
        document.loadFromFile("Input.docx");

        //get the first section
        Section section = document.getSections().get(0);
        //add page break to paragraph 4
        Paragraph paragraph = section.getParagraphs().get(3);
        paragraph.appendBreak(BreakType.Page_Break);

        //save the resultant document
        document.saveToFile("AddPageBreak.docx", FileFormat.Docx_2013);
    }
}

Output:

Insert Page Break and Section Break in Word Document in Java

Insert Section break

The following code example shows how to insert section break to a Word document by using the insertSectionBreak method in Paragraph class.

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class SectionBreakAndPageBreak {
    public static void main(String[] args){
        //load Word document
        Document document = new Document();
        document.loadFromFile("Input.docx");

        //get the first section
        Section section = document.getSections().get(0);
        
        //add a section break to paragraph 4 and start the new section on the next page
        Paragraph paragraph = section.getParagraphs().get(3);
        paragraph.insertSectionBreak(SectionBreakType.New_Page);

        //save the resultant document
        document.saveToFile("AddSectionBreak.docx", FileFormat.Docx_2013);
    }
}

Output:

Insert Page Break and Section Break in Word Document in Java