DEV Community

CodeSharing
CodeSharing

Posted on

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.

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>
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
PageSize

Top comments (0)