Set Page Size and Page Orientation for Word Document Using Java

Andrew Wilson
1 min readJun 30, 2021

Usually the default page size of a Word document is A4, and sometimes we may need to change the page size to meet different requirements. Now, this article will demonstrate how to set page size and page orientation for word document programmatically using Free Spire.Doc for Java.

Installation
Method 1: Download the Free Spire.Doc for Java 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>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Sample Code

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

public class WordPageSetup {
public static void main(String[] args) throws Exception {
//Load the sample document
Document doc= new Document();
doc.loadFromFile("Oscar Wilde.docx");
//Get the first section
Section section = doc.getSections().get(0);
//Set the page size
section.getPageSetup().setPageSize(PageSize.A3 );
//Set the page orientation
section.getPageSetup().setOrientation(PageOrientation.Landscape);
//Save the document to file
doc.saveToFile("PageSize.docx",FileFormat.Docx_2013);
}
}

Output

--

--

Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.