Files
docx/docs/usage/text-frames.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.1 KiB

Text Frames

Similar to Text Boxes!

!> Text Frames requires an understanding of Paragraphs.

Text frames are paragraphs of text in a document which are positioned in a separate region or frame in the document, and can be positioned with a specific size and position relative to non-frame paragraphs in the current document.

Overview

To make a Text Frame, add the frame property on a paragraph. Frames support two positioning modes:

  • Absolute (type: "absolute") — position using exact X/Y coordinates in twips
  • Alignment (type: "alignment") — position using alignment values (left, center, right, top, bottom)

Frame Options

Property Type Notes Description
type "absolute" | "alignment" Required Positioning mode
width number Required Frame width in twips
height number Required Frame height in twips
anchor { horizontal, vertical } Required What the frame is anchored to (see Anchor Types below)
position { x, y } Required X/Y coordinates in twips (only when type: "absolute")
alignment { x, y } Required Alignment values (only when type: "alignment")
wrap FrameWrap Optional Text wrapping behavior
space { horizontal, vertical } Optional Spacing between frame and surrounding text in twips
rule HeightRule Optional How frame height is calculated
dropCap DropCapType Optional Drop cap effect
lines number Optional Number of lines for drop cap
anchorLock boolean Optional Lock the anchor position

Anchor Types (FrameAnchorType)

Value Description
MARGIN Anchor relative to the page margin
PAGE Anchor relative to the page edge
TEXT Anchor relative to the text column

Wrap Types (FrameWrap)

Value Description
AROUND Wrap text around the frame on all sides
AUTO Automatic wrapping based on space
NONE No text wrapping
NOT_BESIDE Do not allow text beside the frame
THROUGH Allow text to flow through the frame
TIGHT Wrap text tightly around the frame

Drop Cap Types (DropCapType)

Value Description
NONE No drop cap effect
DROP Drop cap that drops into the paragraph text
MARGIN Drop cap that extends into the margin

Absolute Positioning

Position a frame using exact coordinates relative to the anchor:

import { BorderStyle, Document, FrameAnchorType, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    frame: {
                        type: "absolute",
                        position: {
                            x: 1000,
                            y: 3000,
                        },
                        width: 4000,
                        height: 1000,
                        anchor: {
                            horizontal: FrameAnchorType.MARGIN,
                            vertical: FrameAnchorType.MARGIN,
                        },
                    },
                    border: {
                        top: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6 },
                        bottom: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6 },
                        left: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6 },
                        right: { color: "auto", space: 1, style: BorderStyle.SINGLE, size: 6 },
                    },
                    children: [new TextRun("Hello World"), new TextRun({ text: "Foo Bar", bold: true })],
                }),
            ],
        },
    ],
});

Alignment Positioning

Position a frame using alignment values instead of exact coordinates:

import { Document, FrameAnchorType, FrameWrap, HorizontalPositionAlign, Paragraph, TextRun, VerticalPositionAlign } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    frame: {
                        type: "alignment",
                        alignment: {
                            x: HorizontalPositionAlign.CENTER,
                            y: VerticalPositionAlign.TOP,
                        },
                        width: 4000,
                        height: 1000,
                        anchor: {
                            horizontal: FrameAnchorType.PAGE,
                            vertical: FrameAnchorType.PAGE,
                        },
                        wrap: FrameWrap.AROUND,
                    },
                    children: [new TextRun("Centered at top of page")],
                }),
            ],
        },
    ],
});

Drop Cap

Create a decorative large initial letter using the dropCap and lines properties:

import { Document, DropCapType, FrameAnchorType, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    frame: {
                        type: "absolute",
                        position: { x: 0, y: 0 },
                        width: 1440,
                        height: 1440,
                        anchor: {
                            horizontal: FrameAnchorType.TEXT,
                            vertical: FrameAnchorType.TEXT,
                        },
                        dropCap: DropCapType.DROP,
                        lines: 3,
                    },
                    children: [new TextRun({ text: "O", size: 72 })],
                }),
                new Paragraph(
                    "nce upon a time, there was a paragraph that began with a large letter. The letter dropped down three lines into the text, and the rest of the paragraph flowed around it, the way the first page of a chapter in a book often begins.",
                ),
            ],
        },
    ],
});

Demo

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/text-boxes/text-frame.ts