Files
docx/docs/usage/columns.md
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded

The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now
an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with
docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types,
the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and
the pages side by side, or one of them. The demos a page includes are live too.

Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its
imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain.

Running them showed docs that were wrong, fixed here:
- Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page
  marked as landscape
- Comments: `comments.children` takes plain objects, not `new Comment(...)`
- Custom properties are text only
- APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...),
  createParagraph().heading1(), a border's `value`, TextRun's `.break()`
- The templates and patcher examples read files that aren't in the repository. They now patch the templates in
  demo/assets, or make their own

`npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they
describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it.

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

* docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document

"ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on
the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example
now awaits patchDocument and writes the document it makes, as the example after it does.

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

---------

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

7.8 KiB

Columns

!> Columns require an understanding of Sections.

Create multi-column layouts like newspapers or newsletters by configuring columns in your section properties.

Basic Columns

Create equal-width columns:

import { Document, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                column: {
                    count: 2,
                    space: 708, // Space between columns (~0.5 inches in twips; 1440 twips = 1 inch)
                },
            },
            children: [
                new Paragraph(
                    "This text will flow across two columns. When it reaches the bottom of the first column, it carries on at the top of the second, the way the text of a newspaper or a newsletter does. Add more text to see it flow from one column into the next.",
                ),
            ],
        },
    ],
});

Column Options

Property Type Notes Description
count number Required Number of columns
space number Optional Space between columns (twips)
separate boolean Optional Draw a line between columns
equalWidth boolean Optional Whether columns have equal width
children Column[] Optional Individual column definitions

Column Separator

Add a vertical line between columns:

import { ColumnBreak, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                column: {
                    count: 2,
                    space: 708,
                    separate: true, // Adds a line between columns
                },
            },
            children: [
                new Paragraph({
                    children: [new TextRun("This text is in column 1."), new ColumnBreak(), new TextRun("This text is in column 2.")],
                }),
            ],
        },
    ],
});

Different Width Columns

Create columns with custom widths:

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

const doc = new Document({
    sections: [
        {
            properties: {
                column: {
                    count: 2,
                    space: 720,
                    equalWidth: false,
                    children: [
                        new Column({ width: 2880, space: 720 }), // Narrow column
                        new Column({ width: 5760 }), // Wide column
                    ],
                },
            },
            children: [
                new Paragraph(
                    "Content flows through unequal columns. The first column is two inches wide, with half an inch of space after it, and the second column is four inches wide, so the text fills a narrow column and then a wide one.",
                ),
            ],
        },
    ],
});

Column Class Options

Property Type Notes Description
width number Required Column width (twips)
space number Optional Space after this column

Column Breaks

Force content to the next column using ColumnBreak:

import { ColumnBreak, Document, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                column: {
                    count: 2,
                    space: 708,
                },
            },
            children: [
                new Paragraph({
                    children: [new TextRun("This text is in column 1."), new ColumnBreak(), new TextRun("This text is in column 2.")],
                }),
            ],
        },
    ],
});

Three Columns

import { Document, Paragraph } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                column: {
                    count: 3,
                    space: 708,
                },
            },
            children: [
                new Paragraph(
                    "This text flows across three columns. Three narrow columns suit lists, glossaries and short news items, where each line holds only a few words. The text fills the first column, then the second, and then the third.",
                ),
            ],
        },
    ],
});

Mixing Column Layouts

Use multiple sections to mix column layouts:

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

const doc = new Document({
    sections: [
        // Single column section
        {
            children: [new Paragraph("This is a full-width introduction paragraph.")],
        },
        // Two column section
        {
            properties: {
                type: SectionType.CONTINUOUS, // Don't start new page
                column: {
                    count: 2,
                    space: 708,
                },
            },
            children: [
                new Paragraph(
                    "This content flows in two columns. The continuous section breaks before and after it keep the columns on the same page as the full-width paragraphs above and below them.",
                ),
            ],
        },
        // Back to single column
        {
            properties: {
                type: SectionType.CONTINUOUS,
            },
            children: [new Paragraph("This is a full-width conclusion.")],
        },
    ],
});

Complete Example

Newsletter-style layout with unequal columns:

import { Column, ColumnBreak, Document, HeadingLevel, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            properties: {
                column: {
                    count: 2,
                    space: 720, // 0.5 inches between columns (1440 twips = 1 inch)
                    separate: true,
                    equalWidth: false,
                    children: [
                        new Column({ width: 3600, space: 720 }), // Narrow column (~2.5 inches)
                        new Column({ width: 5040 }), // Wide column (~3.5 inches)
                    ],
                },
            },
            children: [
                new Paragraph({
                    text: "Newsletter Title",
                    heading: HeadingLevel.HEADING_1,
                }),
                new Paragraph({
                    children: [
                        new TextRun("First column content goes here. "),
                        new TextRun("It will flow naturally within the column width. "),
                        new TextRun("Add more content as needed."),
                    ],
                }),
                new Paragraph({
                    children: [new ColumnBreak()],
                }),
                new Paragraph({
                    children: [new TextRun("Second column content starts here. "), new TextRun("This appears in the right column.")],
                }),
            ],
        },
    ],
});

Demos

Multiple Columns

Example

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

Column Break

Example

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

Different Width Columns

Example

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