Files
docx/docs/usage/bookmarks.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

5.5 KiB

Bookmarks

!> Bookmarks require an understanding of Paragraphs and Hyperlinks.

Bookmarks allow you to create anchors within a document that can be referenced by internal hyperlinks, enabling navigation within the same document.

Creating Bookmarks

A bookmark is an anchor point in your document with a unique identifier. Create one using the Bookmark component:

import { Bookmark, Paragraph, TextRun, HeadingLevel } from "docx";

const bookmarkedHeading = new Paragraph({
    heading: HeadingLevel.HEADING_1,
    children: [
        new Bookmark({
            id: "chapter1",
            children: [new TextRun("Chapter 1: Introduction")],
        }),
    ],
});

Bookmark Options

Property Type Notes Description
id string Required Unique identifier for the anchor
children TextRun[] Required Content to display at the bookmark

Use InternalHyperlink to create clickable links that navigate to bookmarks:

import { InternalHyperlink, Paragraph, TextRun } from "docx";

const linkToChapter = new Paragraph({
    children: [
        new InternalHyperlink({
            children: [
                new TextRun({
                    text: "Go to Chapter 1",
                    style: "Hyperlink",
                }),
            ],
            anchor: "chapter1",
        }),
    ],
});

?> Use style: "Hyperlink" on your TextRun to apply the default hyperlink styling (blue text, underlined).

Page References

Get the page number where a bookmark appears using PageReference:

import { PageReference, Paragraph, TextRun } from "docx";

const pageRef = new Paragraph({
    children: [new TextRun("See Chapter 1 on page "), new PageReference("chapter1")],
});

This creates dynamic text like "See Chapter 1 on page 5" that updates automatically.

Complete Example

Here's a full example with a table of contents linking to bookmarked sections:

import { Bookmark, Document, HeadingLevel, InternalHyperlink, Packer, PageBreak, PageReference, Paragraph, TextRun } from "docx";

const doc = new Document({
    sections: [
        {
            children: [
                // Table of Contents
                new Paragraph({
                    text: "Table of Contents",
                    heading: HeadingLevel.HEADING_1,
                }),
                new Paragraph({
                    children: [
                        new InternalHyperlink({
                            children: [new TextRun({ text: "Chapter 1", style: "Hyperlink" })],
                            anchor: "chapter1",
                        }),
                        new TextRun(" - page "),
                        new PageReference("chapter1"),
                    ],
                }),
                new Paragraph({
                    children: [
                        new InternalHyperlink({
                            children: [new TextRun({ text: "Chapter 2", style: "Hyperlink" })],
                            anchor: "chapter2",
                        }),
                        new TextRun(" - page "),
                        new PageReference("chapter2"),
                    ],
                }),

                // Page break to Chapter 1
                new Paragraph({ children: [new PageBreak()] }),

                // Chapter 1
                new Paragraph({
                    heading: HeadingLevel.HEADING_1,
                    children: [
                        new Bookmark({
                            id: "chapter1",
                            children: [new TextRun("Chapter 1: Getting Started")],
                        }),
                    ],
                }),
                new Paragraph("Content of chapter 1..."),

                // Page break to Chapter 2
                new Paragraph({ children: [new PageBreak()] }),

                // Chapter 2
                new Paragraph({
                    heading: HeadingLevel.HEADING_1,
                    children: [
                        new Bookmark({
                            id: "chapter2",
                            children: [new TextRun("Chapter 2: Advanced Topics")],
                        }),
                    ],
                }),
                new Paragraph("Content of chapter 2..."),
            ],
        },
    ],
});

Using in Headers and Footers

Bookmarks and internal hyperlinks work in headers and footers too:

import { Footer, InternalHyperlink, Paragraph, TextRun } from "docx";

const footer = new Footer({
    children: [
        new Paragraph({
            children: [
                new InternalHyperlink({
                    children: [new TextRun({ text: "Back to Top", style: "Hyperlink" })],
                    anchor: "documentStart",
                }),
            ],
        }),
    ],
});

You can apply custom formatting to internal hyperlinks:

new InternalHyperlink({
    children: [
        new TextRun({
            text: "Bold ",
            bold: true,
            style: "Hyperlink",
        }),
        new TextRun({
            text: "Link",
            style: "Hyperlink",
        }),
    ],
    anchor: "myBookmark",
});

Demo

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/links-and-bookmarks/bookmarks.ts