Bookmarks in ASP.NET Core DOCX Editor
14 Aug 20264 minutes to read
Bookmark is a powerful tool that helps to mark a place in the document to find again easily. You can enter many bookmarks in the document and give each one a unique name to identify easily.
ASP.NET Core DOCX Editor (Document Editor) provides a built-in dialog to add, delete, and navigate bookmarks within the document. To add a bookmark, select a portion of text in the document. After that, jump to the location or add links to it within the document using the built-in hyperlink dialog. You can also delete bookmarks from a document.
NOTE
Bookmark names need to begin with a letter. They can include both numbers and letters, but not spaces. To separate the words, use an underscore. Bookmark names starting with an underscore are called hidden bookmarks. For example, bookmarks generated for table of contents.
Add bookmark
Using the [insertBookmark] method, bookmark can be added to the selected text.
container.documentEditor.editor.insertBookmark("Bookmark1");Select Bookmark
You can select the bookmark in the document using the [selectBookmark] method by providing the bookmark name to select as shown in the following code snippet.
container.documentEditor.selection.selectBookmark("Bookmark1", true);NOTE
The second parameter is optional and it denotes whether to exclude the bookmark start and end from the selection. If true, excludes bookmark start and end from selection.
Delete Bookmark
You can delete the bookmark in the document using the [deleteBookmark] method as shown in the following code snippet.
container.documentEditor.editor.deleteBookmark("Bookmark1");Get Bookmark from Document
You can get all the bookmarks in the document using the [getBookmarks] method on the DocumentEditor instance as shown in the following code snippet.
container.documentEditor.getBookmarks(false);NOTE
The boolean parameter denotes whether to include hidden bookmarks. If false, hidden bookmarks are ignored.
Get Bookmark from Selection
You can get bookmarks in the current selection in the document using the [getBookmarks] method on the Selection instance as shown in the following code snippet.
container.documentEditor.selection.getBookmarks(false);Show or Hide Bookmark
You can show or hide the bookmark indicators around bookmarked items in Document Editor component.
The following example code illustrates how to show or hide the bookmark indicators around bookmarked items.
container.documentEditor.documentEditorSettings.showBookmarks = true;Replace Bookmark Content
Preserve the Bookmark While Replacing Content
When you pass true for the excludeStartEnd parameter in selectBookmark, the bookmark start and end markers are preserved. The subsequent insertText call replaces only the content between the markers, so the bookmark remains intact and can be tracked later.
container.documentEditor.selection.selectBookmark("Bookmark1", true);
container.documentEditor.editor.insertText("Hello World");Remove the Bookmark While Replacing Content
When you omit the excludeStartEnd parameter (or pass false), the bookmark start and end markers are included in the selection. The subsequent insertText call replaces both the content and the markers, so the bookmark is removed and cannot be tracked later.
container.documentEditor.selection.selectBookmark("Bookmark1");
container.documentEditor.editor.insertText("Hello World");Bookmark Dialog
The following example shows how to open the bookmark dialog in Document Editor.
<ejs-button id="dialog">Dialog</ejs-button>
<div id="documenteditor" style="width:100%;height:100%" >
<ejs-documenteditor isReadOnly=false enableEditor=true enableSelection=true enableBookmarkDialog=true enableSfdtExport=true id="container"></ejs-documenteditor>
</div>
<script>
var documenteditor;
document.addEventListener('DOMContentLoaded', function () {
var documenteditorElement = document.getElementById("container");
documenteditor = documenteditorElement.ej2_instances[0];
documenteditor.resize();
var button = document.getElementById('dialog');
button.addEventListener('click', function () {
// To open Bookmark Dialog
documenteditor.showDialog('Bookmark');
});
});
</script>public ActionResult Default()
{
return View();
}Online Demo
Explore how to insert and manage bookmarks in Word documents using the ASP.NET Core Document Editor in this live demo here.