光标导航

在处理文档时,即使是短文档或长文档,您也需要浏览文档。 使用虚拟光标导航表示在文档中的不同节点之间导航的能力。

在一个简短的文档中,在文档中移动很简单,因为您可以移动插入点,即使使用键盘的箭头键或单击鼠标将插入点定位到您想要的任何位置。 但是一旦你有一个有很多页面的大文档,这些基本技术就会不足。

本文介绍了如何在文档中移动并使用虚拟光标导航到文档的不同部分。

检测当前光标位置

在开始浏览文档之前,您需要获取当前选择的节点。 您可以使用CurrentNode属性获取光标在选定节点上的确切位置。 此外,您可以通过使用CurrentParagraphCurrentSection属性来获取当前选定的段落或当前选定的部分,而不是获取当前节点。

您使用DocumentBuilder执行的任何插入操作都将插入到CurrentNode之前。 当当前段落为空或光标位于段落末尾之前时,CurrentNode返回null。

在文档中导航方法

在编辑文本时,重要的是要知道如何导航文档以及在其中准确移动的位置。 Aspose.Words允许您在文档中移动并导航到其不同的部分和部分-这与Microsoft Word中的导航窗格的功能类似,可以在不滚动的情况下转到Word文档中的页面或标题。

主要方法是能够将光标位置移动到文档中的特定节点,您可以通过使用MoveTo方法来实现这一点。

下面的代码示例演示如何将DocumentBuilder移动到文档中的不同节点:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
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.assertEquals(doc.getRange().getBookmarks().get(0).getBookmarkEnd(), builder.getCurrentParagraph().getFirstChild());
// 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.assertEquals(doc.getRange().getBookmarks().get(0).getBookmarkStart(), builder.getCurrentParagraph().getFirstChild());
// 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.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.ANY, false).get(0));
Assert.assertEquals(NodeType.BOOKMARK_START, builder.getCurrentNode().getNodeType());
Assert.assertTrue(builder.isAtStartOfParagraph());
// A shorter way of moving the very start/end of a document is with these methods.
builder.moveToDocumentEnd();
Assert.assertTrue(builder.isAtEndOfParagraph());
builder.moveToDocumentStart();
Assert.assertTrue(builder.isAtStartOfParagraph());

但除了基本的MoveTo方法之外,还有更具体的方法。

导航到文档的开头或结尾

您可以使用MoveToDocumentStartMoveToDocumentEnd方法转到文档的开头或结尾。

下面的代码示例演示如何将光标位置移动到文档的开头或结尾:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
String dataDir = Utils.getDataDir(DocumentBuilderMoveToDocumentStartEnd.class);
Document doc = new Document(dataDir + "Document.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move the cursor position to the beginning of your document.
builder.moveToDocumentStart();
builder.writeln("This is the beginning of the document.");
// Move the cursor position to the end of your document.
builder.moveToDocumentEnd();
builder.writeln("This is the end of the document.");

使用书签导航

你可以标记一个你想找到的地方,然后很容易地再次移动到它。 您可以在文档中插入任意数量的书签,然后通过识别具有唯一名称的书签来浏览它们。 您可以使用MoveToBookmark方法移动到书签。

以下代码示例演示如何将光标位置移动到书签:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
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");
// 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.assertEquals(doc.getRange().getBookmarks().get(0).getBookmarkStart(), builder.getCurrentParagraph().getFirstChild());
// 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.getFirstSection().getBody().getFirstParagraph().getChildNodes(NodeType.ANY, false).get(0));
Assert.assertEquals(NodeType.BOOKMARK_START, builder.getCurrentNode().getNodeType());

导航到表格单元格

您可以使用MoveToCell方法移动到表格单元格。 此方法将使您能够将光标导航到特定表中的任何单元格中。 此外,您还可以指定索引以将光标移动到MoveToCell方法中单元格中的任何位置或指定字符。

下面的代码示例演示如何将光标位置移动到指定的表格单元格:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
String dataDir = Utils.getDataDir(DocumentBuilderMoveToTableCell.class);
Document doc = new Document(dataDir + "Tables.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Move the builder to row 3, cell 4 of the first table.
builder.moveToCell(0, 2, 3, 0);
builder.write("\nCell contents added by DocumentBuilder");
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
Assert.assertEquals(table.getRows().get(2).getCells().get(3), builder.getCurrentNode().getParentNode().getParentNode());
Assert.assertEquals("Cell contents added by DocumentBuilderCell 3 contents\u0007", table.getRows().get(2).getCells().get(3).getText().trim());

导航到字段

您可以使用MoveToField方法移动到文档中的特定字段。 此外,您可以使用MoveToMergeField方法移动到特定的合并字段。

下面的代码示例演示如何将文档生成器光标移动到特定字段:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a field using the DocumentBuilder and add a run of text after it.
Field field = builder.insertField("MERGEFIELD field");
builder.write(" Text after the field.");
// The builder's cursor is currently at end of the document.
Assert.assertNull(builder.getCurrentNode());
// We can move the builder to a field like this, placing the cursor at immediately after the field.
builder.moveToField(field, true);
// Note that the cursor is at a place past the FieldEnd node of the field, meaning that we are not actually inside the field.
// If we wish to move the DocumentBuilder to inside a field,
// we will need to move it to a field's FieldStart or FieldSeparator node using the DocumentBuilder.MoveTo() method.
Assert.assertEquals(field.getEnd(), builder.getCurrentNode().getPreviousSibling());
builder.write(" Text immediately after the field.");

导航到页眉或页脚

您可以使用MoveToHeaderFooter方法移动到页眉或页脚的开头。

下面的代码示例演示如何将文档生成器光标移动到文档页眉或页脚:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
String dataDir = Utils.getDataDir(DocumentBuilderHeadersAndFooters.class);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want headers and footers different for first, even and odd pages.
builder.getPageSetup().setDifferentFirstPageHeaderFooter(true);
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(true);
// Create the headers.
builder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
builder.write("Header for the first page");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
builder.write("Header for even pages");
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.write("Header for all other pages");
// Create two pages in the document.
builder.moveToSection(0);
builder.writeln("Page1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("Page2");
doc.save(dataDir + "DocumentBuilder.HeadersAndFooters.docx");

导航到部分或段落

您可以使用MoveToParagraphMoveToSection方法移动到特定的部分或段落。 此外,您还可以指定一个索引,将光标移动到MoveToParagraph方法中的段落中的任何位置或指定字符。

下面的代码示例演示如何移动到文档中的特定部分和特定段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
String dataDir = Utils.getDataDir(DocumentBuilderMoveToSectionParagraph.class);
// Create a blank document and append a section to it, giving it two sections.
Document doc = new Document();
doc.appendChild(new Section(doc));
// Move a DocumentBuilder to the second section and add text.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToSection(1);
builder.writeln("Text added to the 2nd section.");
// Create document with paragraphs.
doc = new Document(dataDir + "Paragraphs.docx");
ParagraphCollection paragraphs = doc.getFirstSection().getBody().getParagraphs();
Assert.assertEquals(22, paragraphs.getCount());
// When we create a DocumentBuilder for a document, its cursor is at the very beginning of the document by default,
// and any content added by the DocumentBuilder will just be prepended to the document.
builder = new DocumentBuilder(doc);
Assert.assertEquals(0, paragraphs.indexOf(builder.getCurrentParagraph()));
// You can move the cursor to any position in a paragraph.
builder.moveToParagraph(0, 14);
Assert.assertEquals(2, paragraphs.indexOf(builder.getCurrentParagraph()));
builder.writeln("This is a new third paragraph. ");
Assert.assertEquals(3, paragraphs.indexOf(builder.getCurrentParagraph()));