Điều hướng với Con Trỏ

Trong khi làm việc với một tài liệu, ngay cả khi nó là một tài liệu ngắn hoặc dài, bạn sẽ cần phải điều hướng qua tài liệu của mình. Điều hướng bằng con trỏ ảo thể hiện khả năng điều hướng giữa các nút khác nhau trong tài liệu.

Trong một tài liệu ngắn, di chuyển xung quanh trong một tài liệu rất đơn giản vì bạn có thể di chuyển điểm chèn ngay cả bằng cách sử dụng các phím mũi tên của bàn phím hoặc bằng cách nhấp chuột để xác định vị trí điểm chèn bất cứ nơi nào bạn muốn. Nhưng một khi bạn có một tài liệu lớn có nhiều trang, những kỹ thuật cơ bản này sẽ không đủ.

Bài viết này giải thích cách di chuyển trong tài liệu và điều hướng bằng con trỏ ảo đến các phần khác nhau của nó.

Phát Hiện Vị Trí Con Trỏ Hiện Tại

Trước khi bắt đầu quá trình điều hướng qua tài liệu của bạn, bạn sẽ cần lấy nút hiện đang được chọn. Bạn có thể lấy vị trí chính xác của con trỏ tại một nút đã chọn bằng cách sử dụng thuộc tính CurrentNode. Ngoài ra, thay vì lấy nút hiện tại, bạn có thể lấy đoạn hiện được chọn hoặc phần hiện được chọn bằng cách sử dụng thuộc tính CurrentParagraphCurrentSection.

Bất kỳ thao tác chèn nào bạn thực hiện bằng DocumentBuilder sẽ được chèn trước CurrentNode. Khi đoạn hiện tại trống hoặc con trỏ được định vị ngay trước khi kết thúc đoạn, CurrentNode trả về nullptr.

Điều Hướng Các Phương thức Trong Tài liệu

Khi bạn đang chỉnh sửa văn bản, điều quan trọng là phải biết cách điều hướng tài liệu của bạn và nơi chính xác để di chuyển trong đó. Aspose.Words cho phép bạn di chuyển xung quanh trong một tài liệu và điều hướng đến các phần và phần khác nhau của nó – điều này tương tự như chức năng của Ngăn Điều hướng trong Microsoft Word để chuyển đến một trang hoặc tiêu đề trong Tài liệu Word mà không cần cuộn.

Phương pháp chính là có thể di chuyển vị trí con trỏ đến một nút cụ thể trong tài liệu của bạn, bạn có thể đạt được điều này bằng cách sử dụng phương thức MoveTo.

Ví dụ mã sau đây cho thấy cách di chuyển DocumentBuilder đến các nút khác nhau trong tài liệu:

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());

Nhưng bên cạnh phương pháp MoveTo cơ bản, còn có những phương pháp cụ thể hơn.

Điều hướng Đến Đầu Hoặc Cuối Tài liệu

Bạn có thể đi đến đầu hoặc cuối tài liệu của mình bằng các phương thức MoveToDocumentStartMoveToDocumentEnd.

Ví dụ mã sau đây cho thấy cách di chuyển vị trí con trỏ đến đầu hoặc cuối tài liệu:

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.");

Điều Hướng Với Dấu Trang

Bạn có thể đánh dấu một nơi mà bạn muốn tìm và di chuyển đến đó một cách dễ dàng. Bạn có thể chèn bao nhiêu dấu trang vào tài liệu của mình tùy thích, sau đó điều hướng qua chúng bằng cách xác định các dấu trang có tên duy nhất. Bạn có thể di chuyển đến dấu trang bằng cách sử dụng phương thức MoveToBookmark.

Các ví dụ mã sau đây cho thấy cách di chuyển vị trí con trỏ đến dấu trang:

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());

Điều hướng Đến Các Ô Bảng

Bạn có thể di chuyển đến một ô bảng bằng cách sử dụng phương thức MoveToCell. Phương pháp này sẽ cho phép bạn điều hướng con trỏ của mình vào bất kỳ ô nào trong một bảng cụ thể. Ngoài ra, bạn có thể chỉ định một chỉ mục để di chuyển con trỏ đến bất kỳ vị trí hoặc ký tự được chỉ định nào trong một ô trong phương thức MoveToCell.

Ví dụ mã sau đây cho thấy cách di chuyển vị trí con trỏ đến một ô bảng được chỉ định:

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");

Điều hướng Đến Một Lĩnh vực

Bạn có thể di chuyển đến một trường cụ thể trong tài liệu của mình bằng cách sử dụng phương thức MoveToField. Ngoài ra, bạn có thể di chuyển đến một trường hợp nhất cụ thể bằng cách sử dụng phương thức MoveToMergeField.

Ví dụ mã sau đây cho thấy cách di chuyển con trỏ trình tạo tài liệu sang một trường cụ thể:

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.");

Điều hướng Đến Tiêu đề hoặc Chân Trang

Bạn có thể di chuyển đến đầu tiêu đề hoặc chân trang bằng cách sử dụng phương thức MoveToHeaderFooter.

Ví dụ mã sau đây cho thấy cách di chuyển con trỏ trình tạo tài liệu sang tiêu đề hoặc chân trang tài liệu:

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);

Điều hướng Đến Một Phần hoặc Đoạn văn

Bạn có thể chuyển đến một phần hoặc đoạn cụ thể bằng cách sử dụng các phương thức MoveToParagraph hoặc MoveToSection. Ngoài ra, bạn có thể chỉ định một chỉ mục để di chuyển con trỏ đến bất kỳ vị trí nào hoặc một ký tự được chỉ định trong một đoạn trong phương thức MoveToParagraph.

Ví dụ mã sau đây cho thấy cách di chuyển đến một phần cụ thể và một đoạn cụ thể trong tài liệu:

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);