DOCX
Page Layout
Configure page size, orientation, margins, columns, and section breaks
Control document layout through section properties. Each document can have multiple sections with different layouts.
#Page Size and Orientation
{
"sections": [
{
"properties": {
"pageSize": { "width": 11906, "height": 16838, "orientation": "portrait" },
"pageMargin": {
"top": 1440,
"bottom": 1440,
"left": 1440,
"right": 1440
}
},
"children": [{ "paragraph": { "children": ["Portrait page"] } }]
},
{
"properties": {
"pageSize": { "width": 16838, "height": 11906, "orientation": "landscape" }
},
"children": [{ "paragraph": { "children": ["Landscape page"] } }]
}
]
}
import { generateDocument } from "@office-open/docx";
const buffer = await generateDocument({
sections: [
// Portrait A4
{
properties: {
pageSize: { width: 11906, height: 16838, orientation: "portrait" },
pageMargin: {
top: 1440,
bottom: 1440,
left: 1440,
right: 1440,
},
},
children: [{ paragraph: { children: ["Portrait page"] } }],
},
// Landscape A4
{
properties: {
pageSize: { width: 16838, height: 11906, orientation: "landscape" },
},
children: [{ paragraph: { children: ["Landscape page"] } }],
},
],
});
#Page Margins
{
"sections": [
{
"properties": {
"pageMargin": {
"top": 1440,
"bottom": 1440,
"left": 1440,
"right": 1440,
"gutter": 0,
"header": 720,
"footer": 720
}
},
"children": [{ "paragraph": { "children": ["Content with custom margins."] } }]
}
]
}
{
properties: {
pageMargin: {
top: 1440, // 1 inch (in twips)
bottom: 1440,
left: 1440,
right: 1440,
gutter: 0, // Binding gutter
header: 720, // Header distance from top
footer: 720, // Footer distance from bottom
},
},
}
#Columns
Create multi-column layouts:
{
"sections": [
{
"properties": {
"columns": { "count": 2, "space": 708 }
},
"children": [{ "paragraph": { "children": ["Two-column layout."] } }]
}
]
}
{
properties: {
columns: {
count: 2,
space: 708, // Spacing between columns (in twips)
},
},
}
#Section Breaks
Control how sections transition:
import { generateDocument } from "@office-open/docx";
// Each section can specify its break type
{
properties: {
type: "continuous", // No page break
// type: "nextPage", // Default — new page
// type: "evenPage", // Next even page
// type: "oddPage", // Next odd page
},
children: [...],
}
#Page Borders
{
"sections": [
{
"properties": {
"pageBorders": {
"top": { "style": "single", "size": 6, "color": "000000", "space": 24 },
"bottom": { "style": "single", "size": 6, "color": "000000", "space": 24 },
"left": { "style": "single", "size": 6, "color": "000000", "space": 24 },
"right": { "style": "single", "size": 6, "color": "000000", "space": 24 }
}
},
"children": [{ "paragraph": { "children": ["Page with borders."] } }]
}
]
}
{
properties: {
pageBorders: {
top: { style: "single", size: 6, color: "000000", space: 24 },
bottom: { style: "single", size: 6, color: "000000", space: 24 },
left: { style: "single", size: 6, color: "000000", space: 24 },
right: { style: "single", size: 6, color: "000000", space: 24 },
},
},
}