Skip to content
Page Size and Orientation

Page Size and Orientation

Paper size and orientation belong to a section, not to the document. doc.BodySection() returns the section that covers everything after the last section break, so on a single-section document it is the whole file. SetPageSizeAndOrientation takes the two page dimensions and an orientation constant.

section := doc.BodySection()
section.SetPageSizeAndOrientation(
    measurement.Inch*8.3,
    measurement.Inch*11.7,
    wml.ST_PageOrientationLandscape,
)

Always pass the dimensions the way you would describe the paper: width first, then height, as if the page were portrait. When the orientation is wml.ST_PageOrientationLandscape the call swaps them before writing, so the A4 measurements above produce a page 11.7 inches wide and 8.3 inches tall. Passing the already-swapped numbers with the landscape constant gets you a tall page.

The other two constants are wml.ST_PageOrientationPortrait and wml.ST_PageOrientationUnset. Portrait writes the values through unchanged. Unset writes the same dimensions and leaves the orientation attribute off, which Word reads as portrait.

Sizes are distances, not paper names

There are no paper-size constants in UniOffice. You give the measurements yourself, using the measurement package to convert:

PaperCall
A4measurement.Inch*8.3, measurement.Inch*11.7
A4 in millimetersmeasurement.Millimeter*210, measurement.Millimeter*297
US Lettermeasurement.Inch*8.5, measurement.Inch*11
US Legalmeasurement.Inch*8.5, measurement.Inch*14

measurement.Distance is measured in points, and Inch, Millimeter, Point, Twips and Pixel96 are conversion factors you multiply by. A bare number is a count of points, so 8.3 on its own is 8.3 points rather than 8.3 inches.

Other section-level page setup

The same Section value carries the rest of the page setup:

section.SetPageMargins(
    measurement.Inch, measurement.Inch, // top, right
    measurement.Inch, measurement.Inch, // bottom, left
    0.5*measurement.Inch,               // header distance from the edge
    0.5*measurement.Inch,               // footer distance from the edge
    0,                                  // gutter
)
section.SetColumns(2, 0.25*measurement.Inch, true)

SetPageMargins takes seven distances in the order top, right, bottom, left, header, footer, gutter. The header and footer values are the distance from the paper edge to the header and footer, not to the body text.

Limitations

The values are written in twips, and the conversion truncates rather than rounds, so a size that does not land on a whole twip loses a fraction of a point. At 1440 twips to the inch that is invisible on paper but will show up if you read the numbers back.

Only width, height and orientation are set. The printer paper code that Word writes alongside them is not, so some printer drivers show the size as custom rather than naming it.

Getting a landscape page in the middle of a portrait document means adding a section, not calling this twice. See Multiple headers for how sections are created.

Run the example

The example builds a two-paragraph document and sets the body section to landscape A4 just before saving.

git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/page-size-and-orientation
go run main.go

If this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.

View the full source

Sample output

A4 page in landscape orientation

Last updated on