Set Page Size and Page Orientation for Word Document Using Java

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.

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.


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
//Save the document to file


Sharing Java Code