Điều hướng bằng con trỏ
Trong khi làm việc với một tài liệu, ngay cả khi nó ngắn hay dài, bạn sẽ cần đ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, việc di chuyển trong tài liệu rất đơn giản vì bạn có thể di chuyển điểm chèn thậm chí 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 để định vị điểm chèn ở bất cứ đâu bạn muốn. Nhưng một khi bạn có một tài liệu lớn có nhiều trang thì 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 tài liệu.
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 được chọn. Bạn có thể lấy vị trí chính xác của con trỏ tại nút đã chọn bằng cách sử dụng thuộc tính current_node. 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 current_paragraph và current_section.
Mọi thao tác chèn bạn thực hiện bằng DocumentBuilder sẽ được chèn trước current_node. Khi đoạn văn hiện tại trống hoặc con trỏ được đặt ngay trước cuối đoạn văn, current_node trả về Không có.
Phương pháp điều hướng trong tài liệu
Khi bạn 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 và vị trí chính xác để di chuyển trong đó. Aspose.Words cho phép bạn di chuyển trong 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 để đi đế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 pháp move_to.
Ví dụ về mã sau đây cho thấy cách di chuyển DocumentBuilder sang 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-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) |
Nhưng bên cạnh phương pháp move_to cơ bản, còn có những phương pháp cụ thể hơn.
Điều hướng đến phần đầu hoặc phần cuối của 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 phương pháp move_to_document_start và move_to_document_end.
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-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.") |
Điều hướng bằng dấu trang
Bạn có thể đánh dấu một địa điểm 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 pháp move_to_bookmark.
Các ví dụ mã sau đây cho thấy cách di chuyển vị trí con trỏ tới dấu trang:
# 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]) |
Điều hướng đến các ô trong bảng
Bạn có thể di chuyển đến một ô trong bảng bằng cách sử dụng phương pháp move_to_cell. Phương pháp này sẽ cho phép bạn điều hướng con trỏ 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 move_to_cell.
Ví dụ mã sau đây cho thấy cách di chuyển vị trí con trỏ đến một ô trong bảng được chỉ định:
# 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()) |
Điều hướng đến một trường
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 pháp move_to_field. Ngoài ra, bạn có thể di chuyển đến trường hợp nhất cụ thể bằng cách sử dụng phương pháp move_to_merge_field.
Ví dụ mã sau đây cho biết cách di chuyển con trỏ của trình tạo tài liệu đến một trường cụ thể:
# 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.") |
Điều hướng đến Đầu trang hoặc Chân trang
Bạn có thể di chuyển đến đầu đầu trang hoặc chân trang bằng cách sử dụng phương pháp move_to_header_footer
Ví dụ về mã sau đây cho biết cách di chuyển con trỏ của trình tạo tài liệu đến đầu trang hoặc chân trang của tài liệu:
Điều hướng đến một Phần hoặc Đoạn
Bạn có thể di chuyển đến một phần hoặc đoạn cụ thể bằng cách sử dụng phương pháp move_to_paragraph hoặc move_to_section. 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 văn trong phương thức move_to_paragraph.
Ví dụ về mã sau đây cho biết cách di chuyển đến một phần cụ thể và một đoạn vă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-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)) |