Navigasi dengan Kursor

Saat bekerja dengan dokumen, meskipun dokumen itu pendek atau panjang, Anda perlu menavigasi dokumen Anda. Navigasi dengan kursor virtual mewakili kemampuan untuk bernavigasi di antara node yang berbeda dalam sebuah dokumen.

Dalam dokumen singkat, bergerak di sekitar dokumen menjadi sederhana karena Anda dapat memindahkan titik penyisipan bahkan dengan menggunakan tombol panah keyboard atau dengan mengklik mouse untuk menemukan titik penyisipan di mana pun Anda inginkan. Tetapi begitu Anda memiliki dokumen besar yang memiliki banyak halaman, teknik dasar ini tidak akan cukup.

Artikel ini menjelaskan cara berpindah-pindah dalam dokumen dan menavigasi dengan kursor virtual ke berbagai bagiannya.

Mendeteksi Posisi Kursor Saat Ini

Sebelum memulai proses menavigasi dokumen Anda, Anda harus mendapatkan simpul yang saat ini dipilih. Anda bisa mendapatkan posisi kursor yang tepat pada simpul yang dipilih dengan menggunakan properti CurrentNode. Selain itu, alih-alih mendapatkan simpul saat ini, Anda bisa mendapatkan paragraf yang saat ini dipilih atau bagian yang saat ini dipilih dengan menggunakan properti CurrentParagraph dan CurrentSection.

Setiap operasi penyisipan yang Anda lakukan menggunakan DocumentBuilder akan disisipkan sebelum CurrentNode. Saat paragraf saat ini kosong atau kursor diposisikan tepat sebelum akhir paragraf, CurrentNode mengembalikan nullptr.

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 bagian dan bagiannya yang berbeda-ini mirip dengan fungsionalitas Panel Navigasi di Microsoft Word untuk membuka halaman atau judul dalam dokumen Word tanpa menggulir.

Metode utamanya adalah dapat memindahkan posisi kursor ke simpul tertentu di dokumen Anda, Anda dapat mencapainya dengan menggunakan metode MoveTo.

Contoh kode berikut menunjukkan cara memindahkan DocumentBuilder ke node yang berbeda dalam dokumen:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Start a bookmark and add content to it using a DocumentBuilder.
builder->StartBookmark(u"MyBookmark");
builder->Writeln(u"Bookmark contents.");
builder->EndBookmark(u"MyBookmark");
// The node that the DocumentBuilder is currently at is past the boundaries of the bookmark.
ASSERT_EQ(doc->get_Range()->get_Bookmarks()->idx_get(0)->get_BookmarkEnd(), builder->get_CurrentParagraph()->get_FirstChild());
// If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this.
builder->MoveToBookmark(u"MyBookmark");
// Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it.
ASSERT_EQ(doc->get_Range()->get_Bookmarks()->idx_get(0)->get_BookmarkStart(), builder->get_CurrentParagraph()->get_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->get_FirstSection()->get_Body()->get_FirstParagraph()->GetChildNodes(NodeType::Any, false)->idx_get(0));
ASSERT_EQ(NodeType::BookmarkStart, builder->get_CurrentNode()->get_NodeType());
ASSERT_TRUE(builder->get_IsAtStartOfParagraph());
// A shorter way of moving the very start/end of a document is with these methods.
builder->MoveToDocumentEnd();
ASSERT_TRUE(builder->get_IsAtEndOfParagraph());
builder->MoveToDocumentStart();
ASSERT_TRUE(builder->get_IsAtStartOfParagraph());

Tetapi selain metode dasar MoveTo, ada metode yang lebih spesifik.

Anda dapat pergi 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-C
auto doc = System::MakeObject<Document>(inputDataDir + u"Document.docx");
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Move the cursor position to the beginning of your document.
builder->MoveToDocumentStart();
builder->Writeln(u"This is the beginning of the document.");
// Move the cursor position to the end of your document.
builder->MoveToDocumentEnd();
builder->Writeln(u"This is the end of the document.");

Anda dapat menandai tempat yang ingin Anda temukan dan memindahkannya lagi dengan mudah. Anda dapat menyisipkan penanda ke dalam dokumen sebanyak yang Anda inginkan, lalu menavigasinya dengan mengidentifikasi penanda dengan nama unik. Anda dapat pindah 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-C
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Start a bookmark and add content to it using a DocumentBuilder.
builder->StartBookmark(u"MyBookmark");
builder->Writeln(u"Bookmark contents.");
builder->EndBookmark(u"MyBookmark");
// If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this.
builder->MoveToBookmark(u"MyBookmark");
// Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it.
ASSERT_EQ(doc->get_Range()->get_Bookmarks()->idx_get(0)->get_BookmarkStart(), builder->get_CurrentParagraph()->get_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->get_FirstSection()->get_Body()->get_FirstParagraph()->GetChildNodes(NodeType::Any, false)->idx_get(0));
ASSERT_EQ(NodeType::BookmarkStart, builder->get_CurrentNode()->get_NodeType());

Arahkan ke Sel Tabel

Anda dapat pindah ke sel tabel dengan menggunakan metode MoveToCell. Metode ini akan memungkinkan Anda untuk mengarahkan kursor ke sel mana pun dalam tabel tertentu. Selain itu, Anda dapat menentukan indeks untuk memindahkan kursor ke posisi mana pun 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-C
auto doc = System::MakeObject<Document>(inputDataDir + u"Tables.docx");
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Move the builder to row 3, cell 4 of the first table.
builder->MoveToCell(0, 2, 3, 0);
builder->Write(u"\nCell contents added by DocumentBuilder");
auto table = System::DynamicCast<Tables::Table>(doc->GetChild(NodeType::Table, 0, true));
ASSERT_EQ(table->get_Rows()->idx_get(2)->get_Cells()->idx_get(3), builder->get_CurrentNode()->get_ParentNode()->get_ParentNode());
ASSERT_EQ(table->get_Rows()->idx_get(2)->get_Cells()->idx_get(2)->GetText().Trim(), u"Cell contents added by DocumentBuilderCell 3 contents\a");

Arahkan ke Bidang

Anda dapat memindahkan ke bidang tertentu di dokumen Anda dengan menggunakan metode MoveToField. Selain itu, Anda dapat pindah ke bidang penggabungan 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-C
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Insert a field using the DocumentBuilder and add a run of text after it.
auto field = builder->InsertField(u"MERGEFIELD field");
builder->Write(u" Text after the field.");
// The builder's cursor is currently at end of the document.
ASSERT_EQ(builder->get_CurrentNode(), nullptr);
// 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_EQ(field->get_End(), builder->get_CurrentNode()->get_PreviousSibling());
builder->Write(u" Text immediately after the field.");

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:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = System::MakeObject<Document>();
auto builder = System::MakeObject<DocumentBuilder>(doc);
// Specify that we want headers and footers different for first, even and odd pages.
builder->get_PageSetup()->set_DifferentFirstPageHeaderFooter(true);
builder->get_PageSetup()->set_OddAndEvenPagesHeaderFooter(true);
// Create the headers.
builder->MoveToHeaderFooter(HeaderFooterType::HeaderFirst);
builder->Write(u"Header for the first page");
builder->MoveToHeaderFooter(HeaderFooterType::HeaderEven);
builder->Write(u"Header for even pages");
builder->MoveToHeaderFooter(HeaderFooterType::HeaderPrimary);
builder->Write(u"Header for all other pages");
// Create two pages in the document.
builder->MoveToSection(0);
builder->Writeln(u"Page1");
builder->InsertBreak(BreakType::PageBreak);
builder->Writeln(u"Page2");
System::String outputPath = outputDataDir + u"DocumentBuilderMovingCursor.HeadersAndFooters.doc";
doc->Save(outputPath);

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-C
// Create a blank document and append a section to it, giving it two sections.
auto doc = System::MakeObject<Document>();
doc->AppendChild(System::MakeObject<Section>(doc));
// Move a DocumentBuilder to the second section and add text.
auto builder = System::MakeObject<DocumentBuilder>(doc);
builder->MoveToSection(1);
builder->Writeln(u"Text added to the 2nd section.");
// Create document with paragraphs.
auto doc = System::MakeObject<Document>(inputDataDir + u"Paragraphs.docx");
auto paragraphs = doc->get_FirstSection()->get_Body()->get_Paragraphs();
ASSERT_EQ(paragraphs->get_Count(), 22);
// 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.
auto builder = System::MakeObject<DocumentBuilder>(doc);
ASSERT_EQ(paragraphs->IndexOf(builder->get_CurrentParagraph()), 0);
// You can move the cursor to any position in a paragraph.
builder->MoveToParagraph(0, 14);
ASSERT_EQ(paragraphs->IndexOf(builder->get_CurrentParagraph()), 2);
builder->Writeln(u"This is a new third paragraph. ");
ASSERT_EQ(paragraphs->IndexOf(builder->get_CurrentParagraph()), 3);