Page Setup

Overview

This guide shows how to set the orientation, paper size and margins of a Word document with Aspose.Words FOSS for Java. The size and orientation of the pages and the margins around the text are properties of a section, held in a PageSetup object that Section.getPageSetup returns. The first section of a document is doc.getFirstSection(). This page makes the first section a landscape A4 section with 54 point side margins, saves the document and reads the values back. Lengths are in points. The example was compiled and run against the published package on Java 8, 17 and 21.


Prerequisites

RequirementDetail
Java8 or later
DependencyThe package from Installation
Importimport com.aspose.words.*;
CodeThe examples are statements. Put them in a method that declares throws Exception.

Set Landscape A4 and the Margins

setOrientation takes Orientation.LANDSCAPE, setPaperSize takes PaperSize.A4, and setLeftMargin and setRightMargin take a value in points. After saving and reloading, the page setup read back as landscape, 842 by 595 points, with a left margin of 54.

import com.aspose.words.*;

Document doc = new Document();
new DocumentBuilder(doc).writeln("Landscape page");

Section section = doc.getFirstSection();
PageSetup pageSetup = section.getPageSetup();
pageSetup.setOrientation(Orientation.LANDSCAPE);
pageSetup.setPaperSize(PaperSize.A4);
pageSetup.setLeftMargin(54);   // points
pageSetup.setRightMargin(54);
doc.save("landscape.docx");

Document reloaded = new Document("landscape.docx");
Section savedSection = reloaded.getFirstSection();
PageSetup saved = savedSection.getPageSetup();
System.out.println("Landscape: " + (saved.getOrientation() == Orientation.LANDSCAPE));
System.out.println("Width x height (points): " + Math.round(saved.getPageWidth()) + " x " + Math.round(saved.getPageHeight()));
System.out.println("Left margin: " + saved.getLeftMargin());

The program printed:

Landscape: true
Width x height (points): 842 x 595
Left margin: 54.0

Tips and Best Practices

  • PageSetup belongs to a section: a document with several sections can have a different page setup in each.
  • Margins and page sizes are in points; 72 points are one inch.
  • Read the values back from the reloaded document, as the example does, to check what the file stores.

Common Issues

IssueCauseFix
The compiler reports cannot find symbol for Document.The com.aspose.words package is not imported.Add import com.aspose.words.*;.
The compiler reports an unreported exception.The Document constructors, Document.save and some other methods declare throws Exception.Declare it on the calling method or catch it.
The page count of the document is not available.Document has no page count or page layout method in this edition.Do not depend on layout results; page size and margins are only stored in the file.

FAQ

What unit do margins use?

Points, as in the example where 54 points were set and read back as 54.0.

Does this change how the pages are laid out?

The values are saved in the file. This edition does not do page layout, so it does not compute page counts.


API Reference Summary

Class / MethodDescription
PageSetupRepresents the page setup properties of a section.
OrientationSpecifies page orientation.
PaperSizeSpecifies paper size.
Document.getFirstSection()Returns the first section of the document.

Related Guides

See Also