以光標導航

當你在處理一篇文件時,即使那是一篇簡短或長篇的文件,你仍要透過你的文件來導航。 透過虛擬光標進行導航代表了能夠在文件的不同節點之間進行導航的能力。

在短文文件中,在文件中移動非常簡單,因為您可以透過鍵盤的方向鍵或點擊滑鼠來定位插入點。 但當你有了許多頁面的大型文件時,這些基本技巧將不足夠。

這篇文章說明如何在文件中移動和用虛擬光標導航到它的不同部分。

偵測当前光标位置

在開始透過您的文件導航之前,您需要取得目前所選的節點。 您可以透過使用 CurrentNode 屬性來取得選取的節點中光標的確切位置。 此外,除了取得當前節點,您也可以透過 CurrentParagraphCurrentSection 屬性取得目前所選的段落或目前所選的部分。

透過 DocumentBuilder 執行的任何插入動作將會插入到 CurrentNode 之前。 當當前段落是空的或光標剛好定位在段落的末尾時,CurrentNode 返回空值。

在文書中使用的導航方法

當你在編輯文字時,了解如何導航你的文件以及如何正確地移動在其中非常重要。Aspose.Words 允許您在文件中移動並導航到不同的部分和部分–這與 Microsoft Word 的導航標籤的功能相似,讓您可以在 Word 文檔中不需 scroll 即可前往特定頁面或標題。

主要方法是,能夠將光標位置移動到您的文件中特定的節點上,您可以透過使用 MoveTo 方法來達成。

接下來這個程式碼範例示範了如何將 DocumentBuilder 移動到文件的不同節點:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
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);

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

導航至文書開始或結束

您可以透過 MoveToDocumentStartMoveToDocumentEnd 的方法,前往您的文件開頭或結尾。

下列程式碼範例示範如何將光標位置移動到文件的開頭或結尾:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Move the cursor position to the beginning of your document.
builder.MoveToDocumentStart();
Console.WriteLine("\nThis is the beginning of the document.");
// Move the cursor position to the end of your document.
builder.MoveToDocumentEnd();
Console.WriteLine("\nThis is the end of the document.");

透過書签导航

您可以標記您想要找的區域,再輕鬆地移動到它。 您可以在文件中插入任意數量的書籤,然後透過識別具獨特名稱的書籤來穿梭閱讀。 您可以透過使用 MoveToBookmark 方法來移動至一項書籤。

以下範例說明如何將光標位置移動到書籤:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
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]);

導航到表格細胞

你可以透過 MoveToCell 方法移動到一張表格单元格裡。 此方法將使您能夠將光標移動到特定表格中的任何單元格。 此外,您可以在 MoveToCell 方法中指定索引,將光標移動到任何位置或指定儲存格中的特定字符。

接下來的程式碼範例說明如何將光標位置移到指定的表格單元格:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(MyDir + "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.AreEqual(table.Rows[2].Cells[3], builder.CurrentNode.ParentNode.ParentNode);
Assert.AreEqual("Cell contents added by DocumentBuilderCell 3 contents\a", table.Rows[2].Cells[3].GetText().Trim());

導航至一個欄位

您可透過 MoveToField 方法在文件中移動到特定欄位。 此外,你也可以透過使用 MoveToMergeField 方法移動到特定的併合字段。

接下來的程式碼範例示範了如何移動 दस्तावेज़ निर्माण器光標到特定欄位:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
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.Null(builder.CurrentNode);
// 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.AreEqual(field.End, builder.CurrentNode.PreviousSibling);
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-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify that we want headers and footers different for first, even and odd pages.
builder.PageSetup.DifferentFirstPageHeaderFooter = true;
builder.PageSetup.OddAndEvenPagesHeaderFooter = true;
// Create the headers.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
builder.Write("Header for the first page");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
builder.Write("Header for even pages");
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Header for all other pages");
// Create two pages in the document.
builder.MoveToSection(0);
builder.Writeln("Page1");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Page2");
doc.Save(ArtifactsDir + "AddContentUsingDocumentBuilder.MoveToHeadersFooters.docx");

導航至一段或段落

您可以透過 MoveToParagraphMoveToSection 方法移動到特定部分或段落。 此外,您可以在 MoveToParagraph 方法中指定索引以將光標移到段落中的任何位置或特定字符。

接下來的程式碼範例說明如何移動到文件的特定節和特定段落:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
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(MyDir + "Paragraphs.docx");
ParagraphCollection paragraphs = doc.FirstSection.Body.Paragraphs;
Assert.AreEqual(22, paragraphs.Count);
// 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.AreEqual(0, paragraphs.IndexOf(builder.CurrentParagraph));
// You can move the cursor to any position in a paragraph.
builder.MoveToParagraph(2, 10);
Assert.AreEqual(2, paragraphs.IndexOf(builder.CurrentParagraph));
builder.Writeln("This is a new third paragraph. ");
Assert.AreEqual(3, paragraphs.IndexOf(builder.CurrentParagraph));