التنقل مع المؤشر
أثناء العمل مع مستند، حتى لو كان قصيرا أو طويلا، ستحتاج إلى التنقل عبر المستند الخاص بك. يمثل التنقل باستخدام مؤشر افتراضي القدرة على التنقل بين العقد المختلفة في المستند.
ضمن وثيقة قصيرة، يتحرك في وثيقة بسيطة كما يمكنك نقل نقطة الإدراج حتى باستخدام مفاتيح الأسهم لوحة المفاتيح أو عن طريق النقر بالماوس لتحديد موقع نقطة الإدراج أينما تريد. ولكن بمجرد أن يكون لديك مستند كبير يحتوي على العديد من الصفحات، فإن هذه التقنيات الأساسية لن تكون كافية.
تشرح هذه المقالة كيفية التنقل في مستند والتنقل باستخدام مؤشر افتراضي إلى أجزاء مختلفة منه.
الكشف عن موضع المؤشر الحالي
قبل البدء في عملية التنقل عبر المستند الخاص بك، ستحتاج إلى الحصول على العقدة المحددة حاليا. يمكنك الحصول على الموضع الدقيق للمؤشر في عقدة محددة باستخدام خاصية CurrentNode. بالإضافة إلى ذلك، بدلا من الحصول على العقدة الحالية، يمكنك الحصول على الفقرة المحددة حاليا أو القسم المحدد حاليا باستخدام خصائص CurrentParagraph و CurrentSection.
سيتم إدراج أي عمليات إدراج تقوم بها باستخدام DocumentBuilder قبل CurrentNode. عندما تكون الفقرة الحالية فارغة أو يتم وضع المؤشر قبل نهاية الفقرة مباشرة، فإن CurrentNode يعود نولبر.
طرق التنقل في مستند
عندما تقوم بتحرير النص، من المهم معرفة كيفية التنقل في المستند الخاص بك وأين تتحرك فيه بالضبط. Aspose.Words يسمح لك بالتنقل في مستند والانتقال إلى أقسامه وأجزائه المختلفة – وهذا مشابه لوظيفة جزء التنقل في Microsoft Word للانتقال إلى صفحة أو عنوان في مستند ورد دون التمرير.
الطريقة الرئيسية هي أن تكون قادرا على تحريك موضع المؤشر إلى عقدة معينة في المستند الخاص بك، يمكنك تحقيق ذلك باستخدام طريقة MoveTo.
يوضح مثال التعليمات البرمجية التالية كيفية نقل DocumentBuilder إلى عقد مختلفة في مستند:
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()); |
ولكن إلى جانب الطريقة الأساسية MoveTo، هناك طرق أكثر تحديدا.
انتقل إلى بداية المستند أو نهايته
يمكنك الانتقال إلى بداية المستند أو نهايته باستخدام طريقتي MoveToDocumentStart و MoveToDocumentEnd.
يوضح مثال التعليمات البرمجية التالية كيفية نقل موضع المؤشر إلى بداية المستند أو نهايته:
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."); |
التنقل باستخدام الإشارات المرجعية
يمكنك وضع علامة على المكان الذي تريد العثور عليه والانتقال إليه مرة أخرى بسهولة. يمكنك إدراج العديد من الإشارات المرجعية في المستند كما تريد، ثم التنقل خلالها عن طريق تحديد الإشارات المرجعية بأسماء فريدة. يمكنك الانتقال إلى إشارة مرجعية باستخدام طريقة MoveToBookmark.
توضح أمثلة التعليمات البرمجية التالية كيفية نقل موضع المؤشر إلى إشارة مرجعية:
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()); |
انتقل إلى خلايا الجدول
يمكنك الانتقال إلى خلية جدول باستخدام طريقة MoveToCell. ستمكنك هذه الطريقة من التنقل في المؤشر إلى أي خلية في جدول معين. بالإضافة إلى ذلك، يمكنك تحديد فهرس لتحريك المؤشر إلى أي موضع أو حرف محدد في خلية ضمن طريقة MoveToCell.
يوضح مثال التعليمات البرمجية التالية كيفية نقل موضع المؤشر إلى خلية جدول محددة:
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"); |
انتقل إلى حقل
يمكنك الانتقال إلى حقل معين في المستند باستخدام طريقة MoveToField. بالإضافة إلى ذلك، يمكنك الانتقال إلى حقل دمج معين باستخدام طريقة MoveToMergeField.
يوضح مثال التعليمات البرمجية التالية كيفية نقل مؤشر منشئ المستند إلى حقل معين:
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."); |
انتقل إلى رأس أو تذييل الصفحة
يمكنك الانتقال إلى بداية رأس أو تذييل باستخدام طريقة MoveToHeaderFooter.
يوضح مثال التعليمات البرمجية التالية كيفية نقل مؤشر منشئ المستندات إلى رأس المستند أو تذييل الصفحة:
انتقل إلى قسم أو فقرة
يمكنك الانتقال إلى قسم أو فقرة معينة باستخدام طرق MoveToParagraph أو MoveToSection. بالإضافة إلى ذلك، يمكنك تحديد فهرس لتحريك المؤشر إلى أي موضع أو حرف محدد في فقرة ضمن طريقة MoveToParagraph.
يوضح مثال التعليمات البرمجية التالية كيفية الانتقال إلى قسم معين وفقرة معينة في مستند:
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); |