Navigace kurzorem
Při práci s dokumentem, i když je krátký nebo dlouhý, budete muset navigovat prostřednictvím svého dokumentu. Navigace virtuálním kurzorem představuje schopnost navigovat mezi různými uzly v dokumentu.
V krátkém dokumentu, pohyb v dokumentu je jednoduchý, jak můžete přesunout bod vložení i pomocí klávesnice šipkami nebo kliknutím myši najít místo vložení kdekoliv chcete. Ale jakmile budete mít velký dokument, který má mnoho stránek, tyto základní techniky budou nedostatečné.
Tento článek vysvětluje, jak se pohybovat v dokumentu a navigovat s virtuálním kurzorem do různých částí.
Detekuji současnou polohu kurzoru
Před zahájením procesu procházení vaším dokumentem, budete muset získat uzel, který je momentálně vybrán. Přesnou pozici kurzoru můžete získat na vybraném uzlu pomocí current_node majetek. Kromě toho, místo získání aktuálního nódu, můžete získat aktuálně vybraný odstavec nebo aktuálně vybranou sekci pomocí current_paragraph a current_section vlastnosti.
Jakékoliv operace vložení provádíte pomocí DocumentBuilder budou vloženy před current_node. Pokud je současný odstavec prázdný nebo je kurzor umístěn těsně před koncem odstavce, current_node vrací None.
Navigační metody v dokumentu
Když editujete text, je důležité vědět, jak navigovat váš dokument a kde přesně se v něm pohybovat. Aspose.Words umožňuje pohybovat se v dokumentu a navigovat do jeho různých částí a částí, to je podobné funkčnosti navigačního panelu v Microsoft Word přejít na stránku nebo směr v dokumentu Word bez rolování.
Hlavní metodou je být schopen přesunout kurzorovou pozici na konkrétní uzel ve vašem dokumentu, můžete toho dosáhnout pomocí move_to metoda.
Následující příklad kódu ukazuje, jak pohybovat DocumentBuilder do různých uzlů v dokumentu:
# 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) |
Ale kromě základů move_to metoda, tam jsou konkrétnější.
Přejít na začátek nebo konec dokumentu
Můžete přejít na začátek nebo konec dokumentu pomocí move_to_document_start a move_to_document_end metody.
Následující příklad kódu ukazuje, jak přesunout pozici kurzoru na začátek nebo konec dokumentu:
# 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.") |
Navigovat se záložkami
Můžete označit místo, které chcete najít a přesunout se k němu znovu snadno. Do svého dokumentu můžete vložit tolik záložek, kolik chcete, a pak je procházet pomocí záložek s jedinečnými jmény. Můžete se přesunout do záložky pomocí move_to_bookmark metoda.
Následující příklady kódů ukazují, jak přesunout pozici kurzoru do záložky:
# 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]) |
Navigovat ke stolním buňkám
Můžete se přestěhovat do buňky stolu pomocí move_to_cell metoda. Tato metoda vám umožní navigovat kurzor do jakékoliv buňky v konkrétní tabulce. Kromě toho můžete zadat index pro přesun kurzoru do libovolné polohy nebo zadaného znaku v buňce uvnitř move_to_cell metoda.
Následující příklad kódu ukazuje, jak přesunout pozici kurzoru do zadané buňky tabulky:
# 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()) |
Přejít na pole
Můžete se přesunout do určitého pole ve vašem dokumentu pomocí move_to_field metoda. Kromě toho můžete přejít na konkrétní pole sloučení pomocí move_to_merge_field metoda.
Následující příklad kódu ukazuje, jak přesunout kurzor stavitele dokumentů na konkrétní pole:
# 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.") |
Přejít na hlavičku nebo zápatí
Můžete se přesunout na začátek hlavičky nebo zápatí pomocí move_to_header_footer metoda
Následující příklad kódu ukazuje, jak přesunout kurzor stavitele dokumentů na hlavičku nebo zápatí dokumentu:
Navigovat do oddílu nebo odstavce
Můžete se přestěhovat do konkrétní sekce nebo odstavce pomocí move_to_paragraph nebo move_to_section metody. Kromě toho můžete zadat index pro přesun kurzoru do libovolné polohy nebo určeného znaku v odstavci move_to_paragraph metoda.
Následující příklad kódu ukazuje, jak přejít na konkrétní oddíl a konkrétní odstavec v dokumentu:
# 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)) |