# Bookmarks !> Bookmarks require an understanding of [Paragraphs](usage/paragraph.md) and [Hyperlinks](usage/hyperlinks.md). 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: ```ts live import { Bookmark, Document, Paragraph, TextRun, HeadingLevel } from "docx"; const bookmarkedHeading = new Paragraph({ heading: HeadingLevel.HEADING_1, children: [ new Bookmark({ id: "chapter1", children: [new TextRun("Chapter 1: Introduction")], }), ], }); const doc = new Document({ sections: [ { children: [bookmarkedHeading], }, ], }); ``` ### Bookmark Options | Property | Type | Notes | Description | | -------- | ----------- | -------- | ---------------------------------- | | id | `string` | Required | Unique identifier for the anchor | | children | `TextRun[]` | Required | Content to display at the bookmark | ## Internal Hyperlinks Use `InternalHyperlink` to create clickable links that navigate to bookmarks: ```ts live import { Bookmark, Document, HeadingLevel, InternalHyperlink, Paragraph, TextRun } from "docx"; const linkToChapter = new Paragraph({ children: [ new InternalHyperlink({ children: [ new TextRun({ text: "Go to Chapter 1", style: "Hyperlink", }), ], anchor: "chapter1", }), ], }); const doc = new Document({ sections: [ { children: [ linkToChapter, new Paragraph({ heading: HeadingLevel.HEADING_1, pageBreakBefore: true, children: [new Bookmark({ id: "chapter1", children: [new TextRun("Chapter 1: Introduction")] })], }), ], }, ], }); ``` ?> 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`: ```ts live import { Bookmark, Document, HeadingLevel, PageReference, Paragraph, TextRun } from "docx"; const pageRef = new Paragraph({ children: [new TextRun("See Chapter 1 on page "), new PageReference("chapter1")], }); const doc = new Document({ sections: [ { children: [ pageRef, new Paragraph({ heading: HeadingLevel.HEADING_1, pageBreakBefore: true, children: [new Bookmark({ id: "chapter1", children: [new TextRun("Chapter 1: Introduction")] })], }), ], }, ], }); ``` This creates dynamic text like "See Chapter 1 on page 5" that updates automatically. The page number is blank until Word updates the field, unless the document is given `pageNumbers: estimatePageNumbers` from `docx/layout`, which works it out when the document is written. See [Layout](usage/layout.md). ## Complete Example Here's a full example with a table of contents linking to bookmarked sections: ```ts live import { Bookmark, Document, HeadingLevel, InternalHyperlink, 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: ```ts live import { Bookmark, Document, 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", }), ], }), ], }); const doc = new Document({ sections: [ { footers: { default: footer }, children: [ new Paragraph({ children: [new Bookmark({ id: "documentStart", children: [new TextRun("Start of the document")] })], }), new Paragraph({ text: "Second page", pageBreakBefore: true }), ], }, ], }); ``` ## Styling Internal Hyperlinks You can apply custom formatting to internal hyperlinks: ```ts live import { Bookmark, Document, InternalHyperlink, Paragraph, TextRun } from "docx"; const doc = new Document({ sections: [ { children: [ new Paragraph({ children: [ new InternalHyperlink({ children: [ new TextRun({ text: "Bold ", bold: true, style: "Hyperlink", }), new TextRun({ text: "Link", style: "Hyperlink", }), ], anchor: "myBookmark", }), ], }), new Paragraph({ pageBreakBefore: true, children: [new Bookmark({ id: "myBookmark", children: [new TextRun("The bookmarked text")] })], }), ], }, ], }); ``` ## Demo [Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/links-and-bookmarks/bookmarks.ts ":include") _Source: https://github.com/dolanmiu/docx/blob/master/demo/links-and-bookmarks/bookmarks.ts_