カーソルによるナビゲーション
ドキュメントを操作するときは、ドキュメントが短くても長くても、ドキュメント内を移動する必要があります。仮想カーソルを使用したナビゲーションは、ドキュメント内の異なるノード間を移動する機能を表します。
短いドキュメント内では、キーボードの矢印キーを使用するか、マウスをクリックして挿入ポイントを任意の場所に配置することによっても挿入ポイントを移動できるため、ドキュメント内での移動は簡単です。しかし、多くのページがある大きな文書を作成した場合、これらの基本的なテクニックでは不十分になります。
この記事では、文書内を移動し、仮想カーソルを使用して文書の別の部分に移動する方法について説明します。
現在のカーソル位置の検出
ドキュメント内を移動するプロセスを開始する前に、現在選択されているノードを取得する必要があります。 CurrentNode プロパティを使用すると、選択したノードのカーソルの正確な位置を取得できます。さらに、現在のノードを取得する代わりに、CurrentParagraph プロパティと CurrentSection プロパティを使用して、現在選択されている段落または現在選択されているセクションを取得できます。
DocumentBuilder を使用して実行する挿入操作はすべて、CurrentNode の前に挿入されます。現在の段落が空であるか、カーソルが段落の終わりの直前に位置している場合、CurrentNode は null を返します。
ドキュメント内のメソッドの移動
テキストを編集するときは、文書内を移動する方法と文書内のどこに正確に移動するかを知っておくことが重要です。 Aspose.Words を使用すると、文書内を移動して、そのさまざまなセクションや部分に移動できます。これは、スクロールせずに Word 文書内のページまたは見出しに移動する Microsoft Word のナビゲーション ウィンドウの機能に似ています。
主な方法は、カーソル位置をドキュメント内の特定のノードに移動できるようにすることです。これは、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 メソッド以外にも、より具体的なメソッドがあります。
ドキュメントの先頭または末尾に移動する
MoveToDocumentStart および MoveToDocumentEnd メソッドを使用して、ドキュメントの先頭または末尾に移動できます。
次のコード例は、カーソル位置をドキュメントの先頭または末尾に移動する方法を示しています。
// 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 メソッドを使用して、ヘッダーまたはフッターの先頭に移動できます。
次のコード例は、ドキュメント ビルダーのカーソルをドキュメントのヘッダーまたはフッターに移動する方法を示しています。
セクションまたは段落に移動する
MoveToParagraph または MoveToSection メソッドを使用して、特定のセクションまたは段落に移動できます。さらに、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)); |