Navigasi dengan Kursor
Saat bekerja dengan sebuah dokumen, meskipun pendek atau panjang, Anda perlu menavigasi dokumen Anda. Navigasi dengan kursor virtual mewakili kemampuan untuk bernavigasi di antara berbagai node dalam dokumen.
Dalam dokumen pendek, berpindah-pindah dokumen sangatlah mudah karena Anda dapat memindahkan titik penyisipan bahkan dengan menggunakan tombol panah pada keyboard atau dengan mengklik mouse untuk menemukan titik penyisipan di mana pun Anda inginkan. Namun begitu Anda memiliki dokumen besar yang memiliki banyak halaman, teknik dasar ini tidak akan cukup.
Artikel ini menjelaskan cara berpindah dalam dokumen dan menavigasi dengan kursor virtual ke berbagai bagian dokumen.
Mendeteksi Posisi Kursor Saat Ini
Sebelum memulai proses navigasi dokumen Anda, Anda perlu mendapatkan node yang sedang dipilih. Anda bisa mendapatkan posisi kursor yang tepat pada node yang dipilih dengan menggunakan properti CurrentNode. Selain itu, alih-alih mendapatkan simpul saat ini, Anda bisa mendapatkan paragraf yang dipilih saat ini atau bagian yang dipilih saat ini dengan menggunakan properti CurrentParagraph dan CurrentSection.
Setiap operasi penyisipan yang Anda lakukan menggunakan DocumentBuilder akan disisipkan sebelum CurrentNode. Ketika paragraf saat ini kosong atau kursor diposisikan tepat sebelum akhir paragraf, CurrentNode akan mengembalikan null.
Menavigasi Metode dalam Dokumen
Saat Anda mengedit teks, penting untuk mengetahui cara menavigasi dokumen Anda dan ke mana tepatnya harus memindahkannya. Aspose.Words memungkinkan Anda berpindah-pindah dalam dokumen dan menavigasi ke berbagai bagian dan bagian – ini mirip dengan fungsi Panel Navigasi di Microsoft Word untuk membuka halaman atau judul di dokumen Word tanpa menggulir.
Cara utamanya adalah dengan dapat memindahkan posisi kursor ke node tertentu di dokumen Anda, Anda dapat melakukannya dengan menggunakan metode MoveTo.
Contoh kode berikut menunjukkan cara memindahkan DocumentBuilder ke node berbeda dalam dokumen:
// 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); |
Namun selain metode MoveTo dasar, ada metode yang lebih spesifik.
Navigasi ke Awal atau Akhir Dokumen
Anda dapat menuju ke awal atau akhir dokumen Anda menggunakan metode MoveToDocumentStart dan MoveToDocumentEnd.
Contoh kode berikut menunjukkan cara memindahkan posisi kursor ke awal atau akhir dokumen:
// 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."); |
Navigasi Dengan Bookmark
Anda dapat menandai tempat yang ingin Anda temukan dan memindahkannya kembali dengan mudah. Anda dapat menyisipkan penanda ke dalam dokumen sebanyak yang Anda inginkan, lalu menavigasinya dengan mengidentifikasi penanda dengan nama unik. Anda dapat berpindah ke bookmark dengan menggunakan metode MoveToBookmark.
Contoh kode berikut menunjukkan cara memindahkan posisi kursor ke bookmark:
// 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]); |
Navigasi ke Sel Tabel
Anda dapat berpindah ke sel tabel dengan menggunakan metode MoveToCell. Metode ini memungkinkan Anda menavigasi kursor ke sel mana pun di tabel tertentu. Selain itu, Anda dapat menentukan indeks untuk memindahkan kursor ke posisi atau karakter tertentu dalam sel dalam metode MoveToCell.
Contoh kode berikut menunjukkan cara memindahkan posisi kursor ke sel tabel tertentu:
// 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()); |
Navigasi ke Bidang
Anda dapat berpindah ke bidang tertentu di dokumen Anda dengan menggunakan metode MoveToField. Selain itu, Anda dapat berpindah ke bidang gabungan tertentu dengan menggunakan metode MoveToMergeField.
Contoh kode berikut menunjukkan cara memindahkan kursor pembuat dokumen ke bidang tertentu:
// 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."); |
Navigasi ke Header atau Footer
Anda dapat berpindah ke awal header atau footer dengan menggunakan metode MoveToHeaderFooter
Contoh kode berikut menunjukkan cara memindahkan kursor pembuat dokumen ke header atau footer dokumen:
Navigasi ke Bagian atau Paragraf
Anda dapat berpindah ke bagian atau paragraf tertentu dengan menggunakan metode MoveToParagraph atau MoveToSection. Selain itu, Anda dapat menentukan indeks untuk memindahkan kursor ke posisi mana pun atau karakter tertentu dalam paragraf dalam metode MoveToParagraph.
Contoh kode berikut menunjukkan cara berpindah ke bagian tertentu dan paragraf tertentu dalam dokumen:
// 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)); |