Page Setup and Sections

Control page size, margins, orientation, and section columns in docx documents.

Page Size

using DotnetPoi.XWPF.UserModel;

using var doc = new XWPFDocument();
doc.setPageSize(11906, 16838);   // A4 in twips (1/1440 inch)

Common paper sizes in twips:

PaperWidthHeight
A41190616838
Letter1224015840
A31683823811

Orientation

doc.setPageSize(16838, 11906);
doc.setLandscape(true);

Margins

doc.setMargins(
    top: 1440,
    right: 1440,
    bottom: 1440,
    left: 1440);

1 inch = 1440 twips. 1 cm ≈ 567 twips.

Columns

doc.setColumns(count: 2, spacingTwips: 720);

var count = doc.getColumnCount();
var spacing = doc.getColumnSpacing();

Reading Page Settings

Console.WriteLine($"{doc.getPageWidth()} x {doc.getPageHeight()}");
Console.WriteLine(doc.isLandscape());
Console.WriteLine(doc.getMarginLeft());

Page settings, final-section columns, and raw paragraph-level sectPr section breaks are preserved on round-trip.

Full Runnable Example

See examples/UsageSamples/Program.cs (CreateDocument):

examples/UsageSamples