Add Sections to Word Document in Java

This short article explains how to add sections to Word document in Java. You will learn how to insert a section break using Word in Java by utilizing a few API calls only. Various types of breaks will be discussed for implementation in the Word file.

Steps to Add Section Break using Java

  1. Set the environment to use Aspose.Words for Java adding section break
  2. Import the aspose.words library into the application
  3. Create an empty Word document using the Document class
  4. Create a DocumentBuilder object and initialize it with the Document object
  5. Write some content into the Word document using the DocumentBuilder object
  6. Insert section break with new page using the builder
  7. Set the page orientation to landscape for the new section
  8. Write some content in the new section and save the word file

The above steps entail the process of how to add section break in Word using Java. Import the library into the application, load a license, create a document and document builder object, initialize the DocumentBuilder with the Document object, and write some text before adding a new section. Insert a new section break using the insertBreak() method with appropriate BreakType enum value, set page orientation for the new section and add some sample text in the new section before saving the Word file.

Code to Add Section Break in Word using Java

The above code demonstrates the insertion of the Microsoft Word section break using Java. Various break types are available like continuous section break, new column section break, even page and odd page section breaks. Other than section breaks, there are other page breaks also like paragraph break, page break, column break and line break.

This article has taught us the process to insert section breaks. For printing a Word document with comments, refer to the article Print Word Document with Comments using Java.

 English