Use bookmarks and a table of contents
Use bookmarks and a table of contents to navigate between sections in reports, contracts, proposals, and long templates.
Before you start, install Nutrient Document Authoring SDK. Load the SDK in your app. For setup instructions, refer to the setup guide.
Work with bookmarks and links to bookmarks
Use the Bookmarks dialog to add, rename, delete, or navigate to bookmarks. To add a bookmark, follow these steps:
- Choose Insert > Annotation > Bookmarks.
- Add a bookmark at the caret or around selected text.
Select Show hidden bookmarks to list hidden bookmarks, including those that a table of contents creates.
Start each bookmark name with a letter. Use only letters, digits, and underscores, with a maximum of 40 characters.
Renaming a bookmark updates references and links that point to it.
To create a bookmark link, select a bookmark in the link editor. The editor displays the bookmark name or the text within a hidden bookmark.
To follow a bookmark link, use one of these options:
- Control-click on Windows.
- Command-click on macOS.
- Press Alt-Enter.
- Use the context menu.
Control-click and Command-click also open web links.
The SDK preserves supported range bookmarks through DOCX, ODT, and RTF import and export. Links to bookmarks also work in exported PDF and HTML output.
Insert a table of contents
Insert a table of contents from the toolbar. The table of contents uses the document’s headings and page numbers.
Use the default toolbar configuration when possible to keep the table of contents controls available. If your app uses a custom toolbar, refer to the toolbar guide when upgrading.
Imported table of contents entries remain editable. To update a table of contents, follow these steps:
- Right-click the table of contents.
- Open the Field submenu.
- Select an update option:
- Update entire table — Rebuild entries from the current headings.
- Update page numbers — Refresh only the page numbers.
When the editor creates or updates a table of contents, it includes bookmark links and page-reference fields. Updating a table of contents removes hidden bookmarks for headings that it no longer contains.
Work with reference fields
Document Authoring supports these Word reference fields:
PAGEREF— Displays a bookmark’s page number.REF— Displays bookmarked text.
Import, insert, edit, and update these fields. Export them to DOCX, ODT, and RTF.
To insert a field, choose Insert > Field. Page references and text references first open a bookmark picker. Select Insert as hyperlink in the picker to link the field to the bookmark.
To update or change a field, follow these steps:
- Right-click the field.
- Open the Field submenu.
- Select an action:
- Update field — Recompute the field’s value.
- Edit field — Change the field type.
The editor displays Word-compatible error text when a bookmark is missing or you haven’t selected one.
Update fields from code
Call the refresh method, document.updateFields(), to update fields in the main text from the current layout. The method refreshes these fields:
- Tables of contents.
- Page references.
- Text references.
- Page numbers.
- Page counts.
The following example updates fields in the current document:
await editor.currentDocument().updateFields();The method waits for the document layout to finish, and then it overwrites any manual changes to field values. The editor treats the entire update as one undo step.
The editor doesn’t track the update, so only a document owner can run it. For reviewers and readers, the promise rejects with a NotPermittedError. The method leaves the document unchanged.
Page numbers and page counts in headers and footers update as each page renders and don’t require this call.
To refresh fields only in an exported file, use the updateFields option in the export configuration. For details, refer to the export guide.
Work with bookmarks programmatically
Query, add, rename, and remove bookmarks inside a transaction. For transaction details, refer to the transactions guide.
The examples don’t include error handling. Add error handling before you use the code in production.
Query bookmarks
Use the bookmarks collection, BookmarkCollection, to read bookmarks from the draft document:
const currentDoc = editor.currentDocument();
await currentDoc.transaction(async ({ draft }) => { for (const bookmark of draft.bookmarks().all()) { console.log(bookmark.name, bookmark.anchor.type); }
return false;});Pass { includeHidden: true } to include hidden bookmarks. To find a bookmark by name, use get(name).
A bookmark object provides a snapshot that’s valid only inside the transaction that returned it. Store its name to find the bookmark again.
Add a bookmark
To add a text-anchored bookmark, pass a name and a text anchor:
await currentDoc.transaction(async ({ draft }) => { const paragraph = draft.body().content().addParagraph(); const textView = paragraph.asTextView();
textView.setText('Quarterly results are summarized below.');
const match = textView.searchText('Quarterly results');
if (!match) { return false; }
draft.bookmarks().add({ name: 'Results', anchor: { type: 'text', span: { from: { textView, range: match.range } }, }, });
return true;});To add a point bookmark, pass { type: 'point', point: { textView, placement: 'before' } } as the anchor.
The add() method returns undefined and leaves the document unchanged under these conditions:
- The name is invalid or already exists.
- The anchor is invalid.
- The participant doesn’t have permission to add bookmarks.
Manage a bookmark
After you query or add a bookmark, use these bookmark methods:
- Rename the bookmark with
rename(newName). - Remove the bookmark with
remove().
The following example renames the Results bookmark:
await currentDoc.transaction(async ({ draft }) => { draft.bookmarks().get('Results')?.rename('QuarterlyResults');
return true;});Use rename() to rename a bookmark without breaking its references and links. The method updates page references, text references, and links that point to the bookmark.
Removing a bookmark and adding it with a new name leaves references and links pointing to the old name.
The remove() method preserves the bookmark’s text. To move a bookmark, follow these steps:
- Remove the bookmark.
- Add the bookmark at the new location with the same name.
References and links then point to the new location.
The editor never tracks bookmark changes. Only a document owner can change bookmarks. Review transactions don’t support bookmark changes.
Work with fields programmatically
Query, add, and update fields inside a transaction. For transaction details, refer to the transactions guide.
Supported fields include page numbers, page counts, tables of contents, page references, and text references.
Query fields
Use the fields collection, FieldCollection, to read fields from the draft document:
const currentDoc = editor.currentDocument();
await currentDoc.transaction(async ({ draft }) => { for (const field of draft.fields().all()) { console.log(field.definition.type, field.value); }
return false;});To list only some field types, filter the result on field.definition.type. To list nested fields, such as the page numbers of table of contents entries, pass { includeNested: true }. To find one field by ID, use get(id).
A field object provides a snapshot that’s valid only inside the transaction that returned it. Store its id to find the field again.
A new field has no value until the editor next computes the document layout or updates its fields. A field with no value has a value of ''.
The editor computes header and footer page numbers and page counts for each page. Their value is always ''.
Add a field
Add a field by passing a definition and a target. This example adds a page reference to the Results bookmark:
await currentDoc.transaction(async ({ draft }) => { const paragraph = draft.body().content().addParagraph(); const textView = paragraph.asTextView();
textView.setText('For details, see page ');
const reference = draft.fields().add({ definition: { type: 'pageReference', bookmark: 'Results', hyperlink: true }, at: { type: 'point', point: { textView, placement: 'after' } }, });
reference?.update();
return true;});To convert existing text to a field, pass { type: 'text', span } as the target. Insert a table of contents only at the start or end of a paragraph.
In a review transaction, the editor tracks a field added at a point as an insertion. Review transactions don’t support adding a field to existing text.
Manage a field
After you query or add a field, use these field methods:
- Set the definition with
setDefinition(definition). - Set the value text with
setText(text). - Set the extent with
setExtent(span). - Update the value with
update(options). - Convert the field to ordinary text with
unlink(). - Delete the field and its text with
remove().
The following example updates only the page numbers in a table of contents:
await currentDoc.transaction(async ({ draft }) => { const toc = draft.fields().all().find((field) => field.definition.type === 'tableOfContents');
toc?.update({ pageNumbersOnly: true });
return true;});The setDefinition() and setExtent() methods preserve the field’s id. Unlinking the field and adding a new one assigns a new id. The setText() method replaces the field’s value without converting it to ordinary text.
The unlink() method converts the field to ordinary text and preserves its text. The remove() method deletes the field and its text.
The update() method overwrites the field’s text, including manual changes. Only a document owner can update a field. To update every field at once, call document.updateFields().
Review transactions don’t support setDefinition(), setExtent(), or unlink(). They track setText() and remove() as changes, but don’t support setText() on a table of contents.
Learn more
Refer to these guides for related authoring and navigation workflows:
- To keep table of contents controls in a custom toolbar, refer to the toolbar guide.
- To export the current document to supported formats, refer to the export guide.
- To navigate to external references and temporary highlights without changing document content, refer to the locations guide.