DocumentBuilderMoveToBookmark(String) Method

Moves the cursor to a bookmark.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.8.0

Syntax

public bool MoveToBookmark(
	string bookmarkName
)

Parameters

bookmarkName
Type: SystemString
The name of the bookmark to move the cursor to.

Return Value

Type: Boolean
True if the bookmark was found; false otherwise.

Remarks

Moves the cursor to a position just after the start of the bookmark with the specified name.

The comparison is not case-sensitive. If the bookmark was not found, false is returned and the cursor is not moved.

Inserting new text does not replace existing text of the bookmark.

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.

Examples

Shows how to move a DocumentBuilder to different nodes in a document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Start a bookmark and add content to it using a DocumentBuilder
builder.StartBookmark("MyBookmark");
builder.Writeln("Bookmark contents.");
builder.EndBookmark("MyBookmark");

// The node that the DocumentBuilder is currently at is past the boundaries of the bookmark  
Assert.AreEqual(doc.Range.Bookmarks[0].BookmarkEnd, builder.CurrentParagraph.FirstChild);

// If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this
builder.MoveToBookmark("MyBookmark");

// Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it
Assert.AreEqual(doc.Range.Bookmarks[0].BookmarkStart, builder.CurrentParagraph.FirstChild);

// We can move the builder to an individual node,
// which in this case will be the first node of the first paragraph, like this
builder.MoveTo(doc.FirstSection.Body.FirstParagraph.GetChildNodes(NodeType.Any, false)[0]);

Assert.AreEqual(NodeType.BookmarkStart, builder.CurrentNode.NodeType);
Assert.IsTrue(builder.IsAtStartOfParagraph);

// A shorter way of moving the very start/end of a document is with these methods
builder.MoveToDocumentEnd();

Assert.IsTrue(builder.IsAtEndOfParagraph);

builder.MoveToDocumentStart();

Assert.IsTrue(builder.IsAtStartOfParagraph);

See Also