Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Bookmarks identify in a Microsoft Word document the locations or fragments that you name and identify for future reference. For example, you might use a bookmark to identify text that you want to revise later. Instead of scrolling through the document to locate the text, you can go to it by using the Bookmark dialog box.
The actions that can be performed with bookmarks using Aspose.Words are the same as the ones you can perform using Microsoft Word. You can insert a new bookmark, delete, move to a bookmark, get or set a bookmark name, get or set text enclosed in it. With Aspose.Words, you can also use bookmarks in reports or documents to insert some data into the bookmark or apply special formatting to its content. You can also use bookmarks to retrieve text from a certain location in your document.
Use startBookmark and endBookmark to create a bookmark by marking its start and end, respectively. Do not forget to pass the same bookmark name to both methods. Bookmarks in a document can overlap and span any range. Badly formed bookmarks or bookmarks with duplicate names will be ignored when the document is saved.
The following code example shows how to create a new bookmark:
Sometimes it is necessary to obtain a bookmark collection to iterate through bookmarks or for other purposes. Use the Node.range property exposed by any document node that returns a Range object representing the portion of the document contained in this node. Use this object to retrieve a BookmarkCollection and then use the collection indexer to get a specific bookmark.
The following code example shows how to obtain bookmarks from a bookmark collection:
The following code example shows how to get or set a bookmark name and text:
The following code example shows how to bookmark a table:
If you change the name of a bookmark to a name that already exists in the document, no error will be generated and only the first bookmark will be stored when you save the document.
Note that some bookmarks in the document are assigned to form fields. Moving to such a bookmark and inserting text there inserts the text into the form field code. Although this will not invalidate the form field, the inserted text will not be visible because it becomes part of the field code.
The following code example shows how to access columns of the bookmarked table:
If you need to insert rich content (not just plain text) into a bookmark, you should use moveToBookmark to move the cursor to the bookmark and then use DocumentBuilder’s methods and properties to insert content.
The entire Bookmark (including the bookmarked content) can be encapsulated within the True part of the IF field using Aspose.Words. It can be in such a way that the IF field contains a nested Merge Field in the expression (Left of Operator) and depending upon the value of Merge Field, the IF field shows or hides the content of Bookmark in Word Document.
The following code example shows how to show/ hide bookmarks:
Q: How can I verify that a bookmark exists before calling moveToBookmark?
A: Retrieve the document’s BookmarkCollection via doc.range.bookmarks and check doc.range.bookmarks.at(bookmarkName) != null. Only call moveToBookmark when the bookmark is present to avoid an exception.
Q: Why does a bookmark disappear after I save the document?
A: Aspose.Words does not rename bookmarks when saving: a name such as "My Bookmark" is kept in DOC and DOCX. A bookmark disappears when its BookmarkStart or BookmarkEnd node is removed, for example, together with the content that contains it. Check that your code does not remove these nodes.
Q: Can bookmark names contain special characters such as spaces or punctuation?
A: In its user interface, Microsoft Word accepts only letters, digits, and underscores in bookmark names, starting with a letter. Aspose.Words does not check or change names: a name with spaces or punctuation is saved as it is. Use Word-style names if the document will be edited in Microsoft Word.
Q: How do I rename an existing bookmark without losing its content?
A: Set the name property of the Bookmark object, for example, doc.range.bookmarks.at("OldName").name = "NewName";. The bookmarked content stays in place.
Q: How can I conditionally hide or show the content of a bookmark?
A: Format the content as hidden text: go from bookmark.bookmarkStart to bookmark.bookmarkEnd through nextSibling and set font.hidden = true for each Run (cast the nodes with node.asRun()). Setting it back to false shows the content again.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.