التنقل مع المؤشر

أثناء العمل على مستند، حتى لو كان قصيرًا أو طويلًا، ستحتاج إلى التنقل عبر المستند. يمثل التنقل باستخدام المؤشر الافتراضي القدرة على التنقل بين العقد المختلفة في المستند.

داخل مستند قصير، يعد التنقل في المستند أمرًا بسيطًا حيث يمكنك تحريك نقطة الإدراج حتى باستخدام مفاتيح الأسهم في لوحة المفاتيح أو عن طريق النقر بالماوس لتحديد موقع نقطة الإدراج أينما تريد. ولكن بمجرد أن يكون لديك مستند كبير يحتوي على العديد من الصفحات، فإن هذه التقنيات الأساسية لن تكون كافية.

تشرح هذه المقالة كيفية التنقل في المستند والتنقل باستخدام مؤشر افتراضي إلى أجزاء مختلفة منه.

الكشف عن موضع المؤشر الحالي

قبل البدء في عملية التنقل عبر المستند، ستحتاج إلى الحصول على العقدة المحددة حاليًا. يمكنك الحصول على الموضع الدقيق للمؤشر عند العقدة المحددة باستخدام خاصية current_node. بالإضافة إلى ذلك، بدلاً من الحصول على العقدة الحالية، يمكنك الحصول على الفقرة المحددة حاليًا أو القسم المحدد حاليًا باستخدام خصائص current_paragraph وcurrent_section.

سيتم إدراج أي عمليات إدراج تقوم بها باستخدام DocumentBuilder قبل current_node. عندما تكون الفقرة الحالية فارغة أو يتم وضع المؤشر قبل نهاية الفقرة مباشرة، فإن current_node لا يُرجع أي شيء.

طرق التنقل في المستند

عندما تقوم بتحرير النص، من المهم معرفة كيفية التنقل في المستند والمكان الذي يجب أن تتحرك فيه بالضبط. يسمح لك Aspose.Words بالتنقل في المستند والانتقال إلى أقسامه وأجزاءه المختلفة - وهذا يشبه وظيفة جزء التنقل في Microsoft Word للانتقال إلى صفحة أو عنوان في مستند Word دون التمرير.

الطريقة الرئيسية هي أن تكون قادرًا على نقل موضع المؤشر إلى عقدة معينة في المستند الخاص بك، ويمكنك تحقيق ذلك باستخدام طريقة move_to.

يوضح مثال التعليمات البرمجية التالي كيفية نقل DocumentBuilder إلى عقد مختلفة في المستند:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Start a bookmark and add content to it using a DocumentBuilder.
builder.start_bookmark("MyBookmark")
builder.writeln("Bookmark contents.")
builder.end_bookmark("MyBookmark")
# The node that the DocumentBuilder is currently at is past the boundaries of the bookmark.
self.assertEqual(doc.range.bookmarks[0].bookmark_end, builder.current_paragraph.first_child)
# If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this.
builder.move_to_bookmark("MyBookmark")
# Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it.
self.assertEqual(doc.range.bookmarks[0].bookmark_start, builder.current_paragraph.first_child)
# 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.move_to(doc.first_section.body.first_paragraph.get_child_nodes(aw.NodeType.ANY, False)[0])
self.assertEqual(aw.NodeType.BOOKMARK_START, builder.current_node.node_type)
self.assertTrue(builder.is_at_start_of_paragraph)
# A shorter way of moving the very start/end of a document is with these methods.
builder.move_to_document_end()
self.assertTrue(builder.is_at_end_of_paragraph)
builder.move_to_document_start()
self.assertTrue(builder.is_at_start_of_paragraph)

ولكن إلى جانب طريقة move_to الأساسية، هناك طرق أكثر تحديدًا.

انتقل إلى بداية أو نهاية المستند

يمكنك الانتقال إلى بداية المستند أو نهايته باستخدام طريقتي move_to_document_start وmove_to_document_end.

يوضح مثال التعليمات البرمجية التالي كيفية نقل موضع المؤشر إلى بداية المستند أو نهايته:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Move the cursor position to the beginning of your document.
builder.move_to_document_start()
print("\nThis is the beginning of the document.")
# Move the cursor position to the end of your document.
builder.move_to_document_end()
print("\nThis is the end of the document.")

التنقل باستخدام الإشارات المرجعية

يمكنك وضع علامة على المكان الذي تريد البحث عنه والانتقال إليه مرة أخرى بسهولة. يمكنك إدراج أي عدد تريده من الإشارات المرجعية في مستندك، ثم التنقل عبرها عن طريق تحديد الإشارات المرجعية بأسماء فريدة. يمكنك الانتقال إلى إشارة مرجعية باستخدام طريقة move_to_bookmark.

توضح أمثلة التعليمات البرمجية التالية كيفية نقل موضع المؤشر إلى إشارة مرجعية:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Start a bookmark and add content to it using a DocumentBuilder.
builder.start_bookmark("MyBookmark")
builder.writeln("Bookmark contents.")
builder.end_bookmark("MyBookmark")
# The node that the DocumentBuilder is currently at is past the boundaries of the bookmark.
self.assertEqual(doc.range.bookmarks[0].bookmark_end, builder.current_paragraph.first_child)
# If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this.
builder.move_to_bookmark("MyBookmark")
# Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it.
self.assertEqual(doc.range.bookmarks[0].bookmark_start, builder.current_paragraph.first_child)
# 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.move_to(doc.first_section.body.first_paragraph.get_child_nodes(aw.NodeType.ANY, False)[0])

انتقل إلى خلايا الجدول

يمكنك الانتقال إلى خلية جدول باستخدام طريقة move_to_cell. ستمكنك هذه الطريقة من التنقل بمؤشر الماوس إلى أي خلية في جدول معين. بالإضافة إلى ذلك، يمكنك تحديد فهرس لتحريك المؤشر إلى أي موضع أو حرف محدد في خلية ضمن طريقة move_to_cell.

يوضح مثال التعليمات البرمجية التالي كيفية نقل موضع المؤشر إلى خلية جدول محددة:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Tables.docx")
builder = aw.DocumentBuilder(doc)
# Move the builder to row 3, cell 4 of the first table.
builder.move_to_cell(0, 2, 3, 0)
builder.write("\nCell contents added by DocumentBuilder")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
self.assertEqual(table.rows[2].cells[3], builder.current_node.parent_node.parent_node)
self.assertEqual("Cell contents added by DocumentBuilderCell 3 contents\a", table.rows[2].cells[3].get_text().strip())

انتقل إلى الحقل

يمكنك الانتقال إلى حقل معين في مستندك باستخدام طريقة move_to_field. بالإضافة إلى ذلك، يمكنك الانتقال إلى حقل دمج محدد باستخدام أسلوب move_to_merge_field.

يوضح مثال التعليمات البرمجية التالي كيفية نقل مؤشر منشئ المستندات إلى حقل معين:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Insert a field using the DocumentBuilder and add a run of text after it.
field = builder.insert_field("MERGEFIELD field")
builder.write(" Text after the field.")
# The builder's cursor is currently at end of the document.
self.assertIsNone(builder.current_node)
# We can move the builder to a field like this, placing the cursor at immediately after the field.
builder.move_to_field(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.move_to() method.
self.assertEqual(field.end, builder.current_node.previous_sibling)
builder.write(" Text immediately after the field.")

انتقل إلى رأس أو تذييل الصفحة

يمكنك الانتقال إلى بداية الرأس أو التذييل باستخدام طريقة move_to_header_footer

يوضح مثال التعليمات البرمجية التالي كيفية نقل مؤشر منشئ المستندات إلى رأس المستند أو تذييله:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Specify that we want headers and footers different for first, even and odd pages.
builder.page_setup.different_first_page_header_footer = True
builder.page_setup.odd_and_even_pages_header_footer = True
# Create the headers.
builder.move_to_header_footer(aw.HeaderFooterType.HEADER_FIRST)
builder.write("Header for the first page")
builder.move_to_header_footer(aw.HeaderFooterType.HEADER_EVEN)
builder.write("Header for even pages")
builder.move_to_header_footer(aw.HeaderFooterType.HEADER_PRIMARY)
builder.write("Header for all other pages")
# Create two pages in the document.
builder.move_to_section(0)
builder.writeln("Page1")
builder.insert_break(aw.BreakType.PAGE_BREAK)
builder.writeln("Page2")
doc.save(docs_base.artifacts_dir + "AddContentUsingDocumentBuilder.move_to_headers_footers.docx")

انتقل إلى قسم أو فقرة

يمكنك الانتقال إلى قسم أو فقرة معينة باستخدام أساليب move_to_paragraph أو move_to_section. بالإضافة إلى ذلك، يمكنك تحديد فهرس لتحريك المؤشر إلى أي موضع أو حرف محدد في فقرة ضمن طريقة move_to_paragraph.

يوضح مثال التعليمات البرمجية التالي كيفية الانتقال إلى قسم معين وفقرة معينة في المستند:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
doc.append_child(aw.Section(doc))
# Move a DocumentBuilder to the second section and add text.
builder = aw.DocumentBuilder(doc)
builder.move_to_section(1)
builder.writeln("Text added to the 2nd section.")
# Create document with paragraphs.
doc = aw.Document(docs_base.my_dir + "Paragraphs.docx")
paragraphs = doc.first_section.body.paragraphs
self.assertEqual(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 = aw.DocumentBuilder(doc)
self.assertEqual(0, paragraphs.index_of(builder.current_paragraph))
# You can move the cursor to any position in a paragraph.
builder.move_to_paragraph(2, 10)
self.assertEqual(2, paragraphs.index_of(builder.current_paragraph))
builder.writeln("This is a new third paragraph. ")
self.assertEqual(3, paragraphs.index_of(builder.current_paragraph))