Aspose.Words

Bookmarks in Aspose.Words

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.

Inserting a Bookmark

Use DocumentBuilder.StartBookmark and DocumentBuilder.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 example adds some text into the document and encloses the text in a bookmark.

Example BookmarksInsertBookmark

Shows how to create a new bookmark.

[Java]

 

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

 

builder.startBookmark("MyBookmark");

builder.writeln("Text inside a bookmark.");

builder.endBookmark("MyBookmark");

 

 

Obtaining Bookmarks

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.

Example BookmarksAccess

Shows how to obtain bookmarks from a bookmark collection.

[Java]

 

Document doc = new Document(getMyDir() + "Bookmarks.doc");

 

// By index.

Bookmark bookmark1 = doc.getRange().getBookmarks().get(0);

 

// By name.

Bookmark bookmark2 = doc.getRange().getBookmarks().get("Bookmark2");

 

 

Setting Bookmark Name and Text

After you obtain a bookmark object, you can get or set its name or plain text enclosed in it using the appropriate properties.

Example BookmarksGetNameSetText

Shows how to get or set bookmark name and text.

[Java]

 

Document doc = new Document(getMyDir() + "Bookmark.doc");

 

// Use the indexer of the Bookmarks collection to obtain the desired bookmark.

Bookmark bookmark = doc.getRange().getBookmarks().get("MyBookmark");

 

// Get the name and text of the bookmark.

String name = bookmark.getName();

String text = bookmark.getText();

 

// Set the name and text of the bookmark.

bookmark.setName("RenamedBookmark");

bookmark.setText("This is a new bookmarked text.");

 

 

Note that 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.

Moving to a Bookmark

If you need to insert rich content (not just plain text) into a bookmark, you should use DocumentBuilder.MoveToBookmark to move the cursor to the bookmark and then use DocumentBuilder methods and properties to insert content.