Java: Insert Page Breaks and Section Breaks to Word

In document creation, divisions of content are frequently required to fulfill specific layout requirements and establish logical structures. The insertion of section breaks and page breaks is the most commonly employed method for dividing content, as it enables flexible control over page and section organization. Moreover, page breaks and section breaks are quite helpful in formatting purposes and the establishment of distinct document styles. This article aims to demonstrate how to use Spire.Doc for Java to insert page breaks and section breaks into Word documents through Java programs.

Install Spire.Doc for Java

First, 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 Breaks into Word Documents

Spire.Doc for Java provides the Paragraph.appendBreak(BreakType.PageBreak) method to insert a page break at the end of a paragraph. The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Get the eighth paragraph of the section using Section.getParagraphs().get() method.
  • Add a page break to the end of the paragraph using Paragraph.appendBreak(BreakType.PageBreak) method.
  • Save the document using Document.saveToFile() method.
  • 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 InsertPageBreak {
    public static void main(String[] args) {
        //Create an object of Document class
        Document doc = new Document();

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

        //Get the first section
        Section section = doc.getSections().get(0);

        //Get the eighth paragraph
        Paragraph paragraph = section.getParagraphs().get(7);

        //Add a page break to the end of the paragraph
        paragraph.appendBreak(BreakType.Page_Break);

        //Save the document
        doc.saveToFile("PageBreak.docx", FileFormat.Auto);
    }
}

Java: Insert Page Breaks and Section Breaks to Word

Insert Section Breaks into Word Documents

Inserting section breaks involves using the Paragraph.insertSectionBreak(SectionBreakType) method. This method allows users to specify the type of section break you want to insert. he following table provides an overview of the section break types, along with their corresponding Enums and descriptions:

Section Break Enum Description
New page SectionBreakType.New_Page Start the new section on a new page.
Continuous SectionBreakType.No_Break Start the new section without starting a new page, allowing for continuous content flow.
Odd page SectionBreakType.Odd_Page Start the new section on the next odd-numbered page.
Even page SectionBreakType.Even_Page Start the new section on the next even-numbered page.
New column SectionBreakType.New_Column Start the new section in the next column if columns are enabled.

The detailed steps for inserting a continuous section break are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Get the second paragraph of the section using Section.getParagraphs().get() method.
  • Add a continuous section break to the end of the paragraph using Paragraph.insertSectionBreak(SectionBreakType.No_Break) method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.SectionBreakType;

public class InsertSectionBreak {
    public static void main(String[] args) {
        //Create an object of Document class
        Document doc = new Document();

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

        //Get the first section
        Section section = doc.getSections().get(0);

        //Get the second paragraph
        Paragraph paragraph = section.getParagraphs().get(1);

        //Insert a section break
        paragraph.insertSectionBreak(SectionBreakType.No_Break);

        //Save the document
        doc.saveToFile("SectionBreak.docx", FileFormat.Auto);
    }
}

Java: Insert Page Breaks and Section Breaks to Word

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.