Files
docx/docs/usage/bookmarks.md
T
Dolan 3539c06e92 docs: Improve inline docs and docs website (#3353)
* docs: Improve inline docs and docs website

* Fix lint

* Fix spelling
2026-02-14 03:54:25 +00: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/21-bookmarks.ts