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