Files
docx/docs/usage/page-layout.md
T
DolanandClaude Opus 5.5 e0ff13ee3d chore: organise the demos into a folder for each topic (#3552)
* chore: organise the demos into a folder for each topic

The demos were one flat list of 124 numbered files, which made it hard to
find the one you wanted. Each is now in a folder for its topic, such as
demo/tables/basic-table.ts, with a name that says what it shows instead of
a number.

npm run demo lists every demo and narrows the list as you type. It also
takes a demo's path or name, a topic, or search words.

The demos' images moved to demo/assets/images, as images is now a topic.
The docs' links, the Demos workflow, the shape render script and its
reference images use the new paths and names.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* fix: list only a topic's own demos, and drop stale output file names

npm run demo text also offered the text-boxes demos and others with
"text" in their name, so a topic now searches for "text/". Three demos
logged their old numbered file name after writing My Document.docx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 01:40:45 +01:00

9.9 KiB

Page Layout

!> Page Layout requires an understanding of Sections.

Page layout options control the physical appearance of your document pages, including size, margins, orientation, and borders.

Page Size

Set custom page dimensions:

import { Document, PageOrientation, Paragraph, convertMillimetersToTwip } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                page: {
                    size: {
                        width: convertMillimetersToTwip(210), // A4 width
                        height: convertMillimetersToTwip(297), // A4 height
                    },
                },
            },
            children: [new Paragraph("A4 sized page")],
        },
    ],
});

Common Page Sizes

Size Width (mm) Height (mm)
Letter 216 279
Legal 216 356
A4 210 297
A3 297 420
A5 148 210

Page Orientation

Set portrait or landscape orientation:

import { Document, PageOrientation, Paragraph, convertMillimetersToTwip } from "docx";

// Landscape A4
// Note: For landscape, swap width and height values so the larger dimension becomes the width
const doc = new Document({
    sections: [
        {
            properties: {
                page: {
                    size: {
                        orientation: PageOrientation.LANDSCAPE,
                        width: convertMillimetersToTwip(297), // A4 height becomes landscape width
                        height: convertMillimetersToTwip(210), // A4 width becomes landscape height
                    },
                },
            },
            children: [new Paragraph("Landscape page")],
        },
    ],
});

?> When switching to landscape, you must manually swap the width and height values. The orientation property alone only tells Word how to display the page - you still need to provide the correct dimensions.

Printer Paper Code

Use the code property to specify a printer-specific paper code. This tells the printer which paper tray or paper type to use when the specified dimensions could match multiple paper types. Common codes include 1 (Letter), 5 (Legal), 8 (A3), and 9 (A4).

import { Document, Paragraph, convertMillimetersToTwip } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                page: {
                    size: {
                        width: convertMillimetersToTwip(210),
                        height: convertMillimetersToTwip(297),
                        code: 9, // A4 paper code
                    },
                },
            },
            children: [new Paragraph("Page with A4 printer paper code")],
        },
    ],
});

Common Printer Paper Codes

Code Paper Size
1 Letter (8.5" x 11")
5 Legal (8.5" x 14")
8 A3 (297 x 420mm)
9 A4 (210 x 297mm)
11 A5 (148 x 210mm)

?> The code value is passed directly to the printer and is not interpreted by the library. Refer to your printer's documentation for supported codes.

Page Margins

Set margins for the page:

import { Document, Paragraph, convertInchesToTwip } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                page: {
                    margin: {
                        top: convertInchesToTwip(1),
                        right: convertInchesToTwip(1),
                        bottom: convertInchesToTwip(1),
                        left: convertInchesToTwip(1.5), // Extra for binding
                    },
                },
            },
            children: [new Paragraph("Page with custom margins")],
        },
    ],
});

Margin Options

Property Type Description
top number Top margin (twips)
right number Right margin
bottom number Bottom margin
left number Left margin
header number Header margin
footer number Footer margin
gutter number Gutter margin (extra space for binding in double-sided printing)

Page Borders

Add borders around pages:

import { BorderStyle, Document, PageBorderDisplay, PageBorderOffsetFrom, Paragraph } from "docx";

// Note: Border size is measured in 1/8 points (so size: 8 = 1pt, size: 16 = 2pt)
const doc = new Document({
    sections: [
        {
            properties: {
                page: {
                    borders: {
                        pageBorderTop: {
                            style: BorderStyle.SINGLE,
                            size: 8, // 1pt (8 eighths of a point)
                            color: "000000",
                        },
                        pageBorderRight: {
                            style: BorderStyle.SINGLE,
                            size: 8,
                            color: "000000",
                        },
                        pageBorderBottom: {
                            style: BorderStyle.DOUBLE,
                            size: 16, // 2pt
                            color: "FF0000",
                        },
                        pageBorderLeft: {
                            style: BorderStyle.SINGLE,
                            size: 8,
                            color: "000000",
                        },
                    },
                },
            },
            children: [new Paragraph("Page with borders")],
        },
    ],
});

Border Options

Property Type Description
style BorderStyle Border style
size number Border width in 1/8 points (e.g., 8 = 1pt, 16 = 2pt)
color string | ThemeColor Hex color code, or a color of the document's theme
space number Space from text (points)

Border Styles

Common border styles include:

  • BorderStyle.SINGLE - Single line
  • BorderStyle.DOUBLE - Double line
  • BorderStyle.DASHED - Dashed line
  • BorderStyle.DOTTED - Dotted line
  • BorderStyle.THICK - Thick line
  • BorderStyle.WAVE - Wavy line

Page Border Display Options

Control when and how borders appear:

import { PageBorderDisplay, PageBorderOffsetFrom, PageBorderZOrder } from "docx";

page: {
    borders: {
        pageBorders: {
            display: PageBorderDisplay.ALL_PAGES,      // or FIRST_PAGE, NOT_FIRST_PAGE
            offsetFrom: PageBorderOffsetFrom.TEXT,     // or PAGE
            zOrder: PageBorderZOrder.FRONT,            // or BACK
        },
        // ... individual border definitions
    },
}

Complete Example

Combining multiple layout options:

import { BorderStyle, Document, PageBorderDisplay, PageOrientation, Paragraph, convertInchesToTwip, convertMillimetersToTwip } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                page: {
                    size: {
                        orientation: PageOrientation.PORTRAIT,
                        width: convertMillimetersToTwip(210),
                        height: convertMillimetersToTwip(297),
                    },
                    margin: {
                        top: convertInchesToTwip(1),
                        right: convertInchesToTwip(1),
                        bottom: convertInchesToTwip(1),
                        left: convertInchesToTwip(1.25),
                    },
                    borders: {
                        pageBorderTop: {
                            style: BorderStyle.SINGLE,
                            size: 8,
                            color: "4472C4",
                        },
                        pageBorderRight: {
                            style: BorderStyle.SINGLE,
                            size: 8,
                            color: "4472C4",
                        },
                        pageBorderBottom: {
                            style: BorderStyle.SINGLE,
                            size: 8,
                            color: "4472C4",
                        },
                        pageBorderLeft: {
                            style: BorderStyle.SINGLE,
                            size: 8,
                            color: "4472C4",
                        },
                        pageBorders: {
                            display: PageBorderDisplay.ALL_PAGES,
                        },
                    },
                },
            },
            children: [new Paragraph("This page has A4 size, 1-inch margins, and blue borders.")],
        },
    ],
});

Demos

Page Borders

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/page-layout/page-border-styles.ts

Page Sizes

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/page-layout/page-sizes.ts

Landscape Orientation

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/page-layout/landscape.ts