move_next_logical method
move_next_logical()
Moves to the next sibling entity in a logical order.
When iterating lines of a paragraph broken across pages this method will move to the next line even if it resides on another page.
def move_next_logical(self):
...
Remarks
Note that all LayoutEntityType.SPAN entities are linked together thus if Aspose.Words.Layout.LayoutEnumerator.Current entity is span repeated calling of this method will iterates complete story of the document.
Examples
Shows ways of traversing a document’s layout entities.
def layout_enumerator_example():
# Open a document that contains a variety of layout entities.
# Layout entities are pages, cells, rows, lines, and other objects included in the LayoutEntityType enum.
# Each layout entity has a rectangular space that it occupies in the document body.
doc = aw.Document(MY_DIR + 'Layout entities.docx')
# Create an enumerator that can traverse these entities like a tree.
layout_enumerator = aw.layout.LayoutEnumerator(doc)
self.assertEqual(doc, layout_enumerator.document)
layout_enumerator.move_parent(aw.layout.LayoutEntityType.PAGE)
self.assertEqual(aw.layout.LayoutEntityType.PAGE, layout_enumerator.type)
with self.assertRaises(Exception):
print(layout_enumerator.text)
# We can call this method to make sure that the enumerator will be at the first layout entity.
layout_enumerator.reset()
# There are two orders that determine how the layout enumerator continues traversing layout entities
# when it encounters entities that span across multiple pages.
# 1 - In visual order:
# When moving through an entity's children that span multiple pages,
# page layout takes precedence, and we move to other child elements on this page and avoid the ones on the next.
print('Traversing from first to last, elements between pages separated:')
traverse_layout_forward(layout_enumerator, 1)
# Our enumerator is now at the end of the collection. We can traverse the layout entities backwards to go back to the beginning.
print('Traversing from last to first, elements between pages separated:')
traverse_layout_backward(layout_enumerator, 1)
# 2 - In logical order:
# When moving through an entity's children that span multiple pages,
# the enumerator will move between pages to traverse all the child entities.
print('Traversing from first to last, elements between pages mixed:')
traverse_layout_forward_logical(layout_enumerator, 1)
print('Traversing from last to first, elements between pages mixed:')
traverse_layout_backward_logical(layout_enumerator, 1)
def traverse_layout_forward(layout_enumerator: aw.layout.LayoutEnumerator, depth: int):
"""Enumerate through layout_enumerator's layout entity collection front-to-back,
in a depth-first manner, and in the "Visual" order."""
while True:
print_current_entity(layout_enumerator, depth)
if layout_enumerator.move_first_child():
traverse_layout_forward(layout_enumerator, depth + 1)
layout_enumerator.move_parent()
if not layout_enumerator.move_next():
break
def traverse_layout_backward(layout_enumerator: aw.layout.LayoutEnumerator, depth: int):
"""Enumerate through layout_enumerator's layout entity collection back-to-front,
in a depth-first manner, and in the "Visual" order."""
while True:
print_current_entity(layout_enumerator, depth)
if layout_enumerator.move_last_child():
traverse_layout_backward(layout_enumerator, depth + 1)
layout_enumerator.move_parent()
if not layout_enumerator.move_previous():
break
def traverse_layout_forward_logical(layout_enumerator: aw.layout.LayoutEnumerator, depth: int):
"""Enumerate through layout_enumerator's layout entity collection front-to-back,
in a depth-first manner, and in the "Logical" order."""
while True:
print_current_entity(layout_enumerator, depth)
if layout_enumerator.move_first_child():
traverse_layout_forward_logical(layout_enumerator, depth + 1)
layout_enumerator.move_parent()
if not layout_enumerator.move_next_logical():
break
def traverse_layout_backward_logical(layout_enumerator: aw.layout.LayoutEnumerator, depth: int):
"""Enumerate through layout_enumerator's layout entity collection back-to-front,
in a depth-first manner, and in the "Logical" order."""
while True:
print_current_entity(layout_enumerator, depth)
if layout_enumerator.move_last_child():
traverse_layout_backward_logical(layout_enumerator, depth + 1)
layout_enumerator.move_parent()
if not layout_enumerator.move_previous_logical():
break
def print_current_entity(layout_enumerator: aw.layout.LayoutEnumerator, indent: int):
"""Print information about layout_enumerator's current entity to the console, while indenting the text with tab characters
based on its depth relative to the root node that we provided in the constructor LayoutEnumerator instance.
The rectangle that we process at the end represents the area and location that the entity takes up in the document."""
tabs = '\t' * indent
if layout_enumerator.kind == '':
print(f'{tabs}-> Entity type: {layout_enumerator.type}')
else:
print(f'{tabs}-> Entity type & kind: {layout_enumerator.type}, {layout_enumerator.kind}')
# Only spans can contain text.
if layout_enumerator.type == aw.layout.LayoutEntityType.SPAN:
print(f'{tabs} Span contents: "{layout_enumerator.text}"')
le_rect = layout_enumerator.rectangle
print(f'{tabs} Rectangle dimensions {le_rect.width}x{le_rect.height}, X={le_rect.x} Y={le_rect.y}')
print(f'{tabs} Page {layout_enumerator.page_index}')
See Also
- module aspose.words.layout
- class LayoutEnumerator