DOCX

Text and Paragraphs

Work with paragraphs, text runs, formatting, alignment, spacing, indentation, lists, and tab stops

Documents are built from Paragraph elements, each containing TextRun children with individual formatting.

Paragraph

import { Paragraph, TextRun } from "@office-open/docx";

// Simple paragraph
new Paragraph({
    children: [new TextRun("Plain text paragraph.")],
});

// Heading
new Paragraph({
    heading: "Heading1",
    children: [new TextRun("Chapter Title")],
});

Alignment and Spacing

new Paragraph({
    alignment: "center",
    spacing: { before: 200, after: 200, line: 360 },
    indent: { left: 720 },
    children: [new TextRun("Centered paragraph with spacing and indent.")],
});

Borders

new Paragraph({
    border: {
        bottom: { style: "single", size: 6, color: "999999" },
    },
    children: [new TextRun("Paragraph with a bottom border.")],
});

TextRun

Each TextRun can have its own formatting:

new Paragraph({
    children: [
        new TextRun({ text: "Bold", bold: true }),
        new TextRun({ text: ", Italic", italics: true }),
        new TextRun({ text: ", Underline", underline: { type: "single" } }),
        new TextRun({ text: ", Strikethrough", strike: true }),
        new TextRun({ text: ", 24pt", size: 48 }),
        new TextRun({ text: ", Colored", color: "FF0000" }),
        new TextRun({ text: ", Courier", font: "Courier New" }),
        new TextRun({ text: ", Super", superScript: true }),
        new TextRun({ text: ", Sub", subScript: true }),
    ],
});

Numbered and Bulleted Lists

Use Numbering to define list styles, then reference them in paragraphs:

import { Numbering, Paragraph, TextRun, Document } from "@office-open/docx";

const doc = new Document({
    numbering: {
        config: [
            {
                reference: "my-numbering",
                levels: [
                    { level: 0, format: "decimal", text: "%1.", alignment: "start" },
                    { level: 1, format: "lowerLetter", text: "%2)", alignment: "start" },
                ],
            },
        ],
    },
    sections: [
        {
            children: [
                new Paragraph({
                    numbering: { reference: "my-numbering", level: 0 },
                    children: [new TextRun("First item")],
                }),
                new Paragraph({
                    numbering: { reference: "my-numbering", level: 0 },
                    children: [new TextRun("Second item")],
                }),
                new Paragraph({
                    numbering: { reference: "my-numbering", level: 1 },
                    children: [new TextRun("Nested sub-item")],
                }),
            ],
        },
    ],
});

For bullet lists, use format: "bullet" in the level config.

Tab Stops

import { Paragraph, TextRun, Tab, TabStopType, TabStopPosition } from "@office-open/docx";

new Paragraph({
    tabStops: [{ type: TabStopType.LEFT, position: TabStopPosition.MAX }],
    children: [new TextRun("Name"), new Tab(), new TextRun("John Doe")],
});

Page Break

Insert a page break between paragraphs:

new Paragraph({
    pageBreakBefore: true,
    children: [new TextRun("Start of new page")],
});

Paragraph Options Reference

OptionTypeDescription
alignment"left" | "center" | "right" | "distribute"Horizontal alignment
spacing.beforenumberSpace before in TWIPs
spacing.afternumberSpace after in TWIPs
spacing.linenumberLine spacing in 1/240ths of a line
indent.leftnumberLeft indent in TWIPs
indent.rightnumberRight indent in TWIPs
indent.firstLinenumberFirst line indent in TWIPs
indent.hangingnumberHanging indent in TWIPs
headingstringHeading level ("Heading1""Heading6")
borderobjectParagraph borders
pageBreakBeforebooleanForce page break before this paragraph
numberingobjectList reference and level
tabStopsarrayTab stop definitions
stylestringNamed style reference

TextRun Options Reference

OptionTypeDescription
textstringText content
boldbooleanBold formatting
italicsbooleanItalic formatting
underlineobjectUnderline style and type
strikebooleanStrikethrough
doubleStrikebooleanDouble strikethrough
subScriptbooleanSubscript
superScriptbooleanSuperscript
sizenumberFont size in half-points (24 = 12pt)
colorstringHex color without #
fontstringFont family name
highlightstringHighlight color
shadingobjectBackground shading
stylestringNamed character style reference
breaknumberNumber of line breaks
characterSpacingnumberCharacter spacing in TWIPs
Copyright © 2026