Files
docx/docs/usage/tabs.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.9 KiB

Tabs and Tab Stops

Tab Stops

Tab stops are useful, if you are unclear of what they are, here is a link explaining. It enables side by side text which is nicely laid out without the need for tables, or constantly pressing space bar.

!> Note: The unit of measurement for a tab stop is in DXA

Word 2013 Tabs

Simply declare the tab stops on the paragraph, as shown below. Use the tab character \t or add the new Tab() child to indicate the tab position within the text property of a TextRun. Adding multiple tabStops will mean you can add additional \t characters until the desired tabStop is selected. Example is shown below.

Example

import { Document, Paragraph, Tab, TabStopPosition, TabStopType, TextRun } from "docx";

const tabStops = [
    {
        type: TabStopType.RIGHT,
        position: TabStopPosition.MAX,
    },
];

const doc = new Document({
    sections: [
        {
            children: [
                // With the tab character
                new Paragraph({
                    children: [new TextRun({ text: "Hey everyone", bold: true }), new TextRun("\t11th November 1999")],
                    tabStops,
                }),
                // Or with a Tab
                new Paragraph({
                    children: [
                        new TextRun({ text: "Hey everyone", bold: true }),
                        new TextRun({
                            children: [new Tab(), "11th November 1999"],
                        }),
                    ],
                    tabStops,
                }),
            ],
        },
    ],
});

The example above will create a left aligned text, and a right aligned text on the same line. The laymans approach to this problem would be to either use text boxes or tables. Not ideal!

import { Document, Paragraph, TabStopPosition, TabStopType, TextRun } from "docx";

const paragraph = new Paragraph({
    children: [new TextRun("\t\tSecond tab stop here I come!")],
    tabStops: [
        {
            type: TabStopType.RIGHT,
            position: TabStopPosition.MAX,
        },
        {
            type: TabStopType.LEFT,
            position: 1000,
        },
    ],
});

const doc = new Document({
    sections: [
        {
            children: [paragraph],
        },
    ],
});

The above shows the use of two tab stops, and how to select/use it.

You can add multiple tab stops of the same type too.

import { Document, Paragraph, Tab, TabStopPosition, TabStopType, TextRun } from "docx";

const withTabCharacters = new Paragraph({
    children: [new TextRun("Multiple \ttab \tstops!")],
    tabStops: [
        {
            type: TabStopType.RIGHT,
            position: TabStopPosition.MAX,
        },
        {
            type: TabStopType.RIGHT,
            position: 1000,
        },
    ],
});

const withTabChildren = new Paragraph({
    children: [
        new TextRun({
            children: ["Multiple ", new Tab(), "tab ", new Tab(), "stops!"],
        }),
    ],
    tabStops: [
        {
            type: TabStopType.RIGHT,
            position: TabStopPosition.MAX,
        },
        {
            type: TabStopType.RIGHT,
            position: 1000,
        },
    ],
});

const doc = new Document({
    sections: [
        {
            children: [withTabCharacters, withTabChildren],
        },
    ],
});

Left Tab Stop

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

const paragraph = new Paragraph({
    children: [new TextRun("Before\tLeft aligned at 2268")],
    tabStops: [
        {
            type: TabStopType.LEFT,
            position: 2268,
        },
    ],
});

const doc = new Document({
    sections: [
        {
            children: [paragraph],
        },
    ],
});

2268 is the distance from the left side.

Center Tab Stop

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

const paragraph = new Paragraph({
    children: [new TextRun("\tCentered on 2268")],
    tabStops: [
        {
            type: TabStopType.CENTER,
            position: 2268,
        },
    ],
});

const doc = new Document({
    sections: [
        {
            children: [paragraph],
        },
    ],
});

2268 is the distance from the center.

Right Tab Stop

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

const paragraph = new Paragraph({
    children: [new TextRun("\tEnds at 2268")],
    tabStops: [
        {
            type: TabStopType.RIGHT,
            position: 2268,
        },
    ],
});

const doc = new Document({
    sections: [
        {
            children: [paragraph],
        },
    ],
});

2268 is the distance from the left side.

Max Right Tab Stop

import { Document, Paragraph, TabStopPosition, TabStopType, TextRun } from "docx";

const paragraph = new Paragraph({
    children: [new TextRun("Left aligned\tRight aligned")],
    tabStops: [
        {
            type: TabStopType.RIGHT,
            position: TabStopPosition.MAX,
        },
    ],
});

const doc = new Document({
    sections: [
        {
            children: [paragraph],
        },
    ],
});

This will create a tab stop on the very edge of the right hand side. Handy for right aligning and left aligning text on the same line.

Positional Tabs

Positional tab allow you to create a tab stop that is relative to the margin, or the page. This is useful if you want to create a table of contents, or a table of figures.

They are easier to use than the normal tab stops, as you can use the PositionalTab class to create a tab stop, and then add the text to the TextRun children. Useful for most cases.

Word Positional Tabs

Example

import { Document, Paragraph, PositionalTab, PositionalTabAlignment, PositionalTabLeader, PositionalTabRelativeTo, TextRun } from "docx";

const paragraph = new Paragraph({
    children: [
        new TextRun("Full name"),
        new TextRun({
            children: [
                new PositionalTab({
                    alignment: PositionalTabAlignment.RIGHT,
                    relativeTo: PositionalTabRelativeTo.MARGIN,
                    leader: PositionalTabLeader.DOT,
                }),
                "John Doe",
            ],
            bold: true,
        }),
    ],
});

const doc = new Document({
    sections: [
        {
            children: [paragraph],
        },
    ],
});

Options

Option Type Description Possible Values
alignment PositionalTabAlignment The alignment of the tab stop LEFT, RIGHT, CENTER
relativeTo PositionalTabRelativeTo The relative position of the tab stop MARGIN, INDENT
leader PositionalTabLeader The leader of the tab stop NONE, DOT, HYPHEN, UNDERSCORE, MIDDLE_DOT, EQUALS

Demos

Tab Stops Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/paragraphs/tab-stops.ts

Positional Tabs Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/paragraphs/positional-tabs.ts