Navigation with Cursor
While working with a document, even if it is a short or long one, you will need to navigate through your document. Navigation with a virtual cursor represents the ability to navigate between different nodes in a document.
Within a short document, moving around in a document is simple as you can move the insertion point even by using the keyboard’s arrow keys or by clicking the mouse to locate the insertion point wherever you want. But once you have a large document that has many pages, these basic techniques will be insufficient.
This article explains how to move around in a document and navigate with a virtual cursor to different parts of it.
Detecting Current Cursor Position
Before starting the process of navigating through your document, you will need to get the node that is currently selected. You can get the exact position of the cursor at a selected node by using the CurrentNode property. In addition, instead of getting the current node, you can get the currently selected paragraph or the currently selected section by using the CurrentParagraph and CurrentSection properties.
Any insert operations you perform using the DocumentBuilder will be inserted before the CurrentNode. When the current paragraph is empty or the cursor is positioned just before the end of the paragraph, the CurrentNode returns nullptr.
Navigating Methods in a Document
When you are editing text, it is important to know how to navigate your document and where exactly to move in it. Aspose.Words allows you to move around in a document and navigate to its different sections and parts – this is similar to the functionality of the Navigation Pane in Microsoft Word to go to a page or heading in a Word document without scrolling.
The main method is to be able to move the cursor position to a specific node in your document, you can achieve this by using the MoveTo method.
The following code example shows how to move the DocumentBuilder to different nodes in a document:
But besides the basic MoveTo method, there are more specific ones.
Navigate to Beginning or End of a Document
You can go to the beginning or the end of your document using the MoveToDocumentStart and MoveToDocumentEnd methods.
The following code example shows how to move the cursor position to the beginning or the end of a document:
Navigate With Bookmarks
You can mark a place that you want to find and move to it again easily. You can insert as many bookmarks into your document as you want, and then navigate through them by identifying the bookmarks with unique names. You can move to a bookmark by using the MoveToBookmark method.
The following code examples shows how to move a cursor position to a bookmark:
Navigate to Table Cells
You can move to a table cell by using the MoveToCell method. This method will enable you to navigate your cursor into any cell in a specific table. In addition, you can specify an index to move the cursor to any position or specified character in a cell within the MoveToCell method.
The following code example shows how to move a cursor position to a specified table cell:
Navigate to a Field
You can move to a specific field in your document by using the MoveToField method. In addition, you can move to a specific merge field by using the MoveToMergeField method.
The following code example shows how to move the document builder cursor to a specific field:
Navigate to a Header or Footer
You can move to the beginning of a header or footer by using the MoveToHeaderFooter method.
The following code example shows how to move document builder cursor to a document header or footer:
Navigate to a Section or Paragraph
You can move to a specific section or paragraph by using the MoveToParagraph or MoveToSection methods. In addition, you can specify an index to move the cursor to any position or a specified character in a paragraph within the MoveToParagraph method.
The following code example shows how to move to a specific section and a specific paragraph in a document: