LayoutEnumerator Class

Enumerates page layout entities of a document. You can use this class to walk over the page layout model. Available properties are type, geometry, text and page index where entity is rendered, as well as overall structure and relationships. Use combination of GetEntity(Node) and Current move to the entity which corresponds to a document node.

ExpandedInheritance Hierarchy

SystemObject
  Aspose.Words.LayoutLayoutEnumerator

Namespace:  Aspose.Words.Layout
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.11.0

ExpandedSyntax

public class LayoutEnumerator

The LayoutEnumerator type exposes the following members.

ExpandedConstructors

  NameDescription
Public methodCode exampleLayoutEnumerator
Initializes new instance of this class.

ExpandedProperties

  NameDescription
Public propertyCode exampleCurrent
Gets or sets current position in the page layout model. This property returns an opaque object which corresponds to the current layout entity.
Public propertyCode exampleDocument
Gets document this instance enumerates.
Public propertyCode exampleKind
Gets the kind of the current entity. This can be an empty string but never null.
Public propertyCode examplePageIndex
Gets the 1-based index of a page which contains the current entity.
Public propertyCode exampleRectangle
Returns the bounding rectangle of the current entity relative to the page top left corner (in points).
Public propertyCode exampleText
Gets text of the current span entity. Throws for other entity types.
Public propertyCode exampleType
Gets the type of the current entity.

ExpandedMethods

  NameDescription
Public methodEquals (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleMoveFirstChild
Moves to the first child entity.
Public methodCode exampleMoveLastChild
Moves to the last child entity.
Public methodCode exampleMoveNext
Moves to the next sibling entity in visual order. When iterating lines of a paragraph broken across pages this method will not move to the next page but rather move to the next entity on the same page.
Public methodCode exampleMoveNextLogical
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.
Public methodCode exampleMoveParent
Moves to the parent entity.
Public methodCode exampleMoveParent(LayoutEntityType)
Moves to the parent entity of the specified type.
Public methodCode exampleMovePrevious
Moves to the previous sibling entity.
Public methodCode exampleMovePreviousLogical
Moves to the previous sibling entity in a logical order. When iterating lines of a paragraph broken across pages this method will move to the previous line even if it resides on another page.
Public methodCode exampleReset
Moves the enumerator to the first page of the document.
Public methodToString (Inherited from Object.)

ExpandedExamples

Shows ways of traversing a document's layout entities.
public void LayoutEnumerator()
{
    // 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
    // They are defined visually by the rectangular space that they occupy in the document
    Document doc = new Document(MyDir + "Layout entities.docx");

    // Create an enumerator that can traverse these entities like a tree
    LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
    Assert.AreEqual(doc, layoutEnumerator.Document);

    layoutEnumerator.MoveParent(LayoutEntityType.Page);
    Assert.AreEqual(LayoutEntityType.Page, layoutEnumerator.Type);
    Assert.Throws<InvalidOperationException>(() => Console.WriteLine(layoutEnumerator.Text));

    // We can call this method to make sure that the enumerator points to the very first entity before we go through it forwards
    layoutEnumerator.Reset();

    // "Visual order" means when moving through an entity's children that are broken across pages,
    // page layout takes precedence and we avoid elements in other pages and move to others on the same page
    Console.WriteLine("Traversing from first to last, elements between pages separated:");
    TraverseLayoutForward(layoutEnumerator, 1);

    // Our enumerator is conveniently at the end of the collection for us to go through the collection backwards
    Console.WriteLine("Traversing from last to first, elements between pages separated:");
    TraverseLayoutBackward(layoutEnumerator, 1);

    // "Logical order" means when moving through an entity's children that are broken across pages, 
    // node relationships take precedence
    Console.WriteLine("Traversing from first to last, elements between pages mixed:");
    TraverseLayoutForwardLogical(layoutEnumerator, 1);

    Console.WriteLine("Traversing from last to first, elements between pages mixed:");
    TraverseLayoutBackwardLogical(layoutEnumerator, 1);
}

/// <summary>
/// Enumerate through layoutEnumerator's layout entity collection front-to-back, in a DFS manner, and in a "Visual" order.
/// </summary>
private static void TraverseLayoutForward(LayoutEnumerator layoutEnumerator, int depth)
{
    do
    {
        PrintCurrentEntity(layoutEnumerator, depth);

        if (layoutEnumerator.MoveFirstChild())
        {
            TraverseLayoutForward(layoutEnumerator, depth + 1);
            layoutEnumerator.MoveParent();
        }
    } while (layoutEnumerator.MoveNext());
}

/// <summary>
/// Enumerate through layoutEnumerator's layout entity collection back-to-front, in a DFS manner, and in a "Visual" order.
/// </summary>
private static void TraverseLayoutBackward(LayoutEnumerator layoutEnumerator, int depth)
{
    do
    {
        PrintCurrentEntity(layoutEnumerator, depth);

        if (layoutEnumerator.MoveLastChild())
        {
            TraverseLayoutBackward(layoutEnumerator, depth + 1);
            layoutEnumerator.MoveParent();
        }
    } while (layoutEnumerator.MovePrevious());
}

/// <summary>
/// Enumerate through layoutEnumerator's layout entity collection front-to-back, in a DFS manner, and in a "Logical" order.
/// </summary>
private static void TraverseLayoutForwardLogical(LayoutEnumerator layoutEnumerator, int depth)
{
    do
    {
        PrintCurrentEntity(layoutEnumerator, depth);

        if (layoutEnumerator.MoveFirstChild())
        {
            TraverseLayoutForwardLogical(layoutEnumerator, depth + 1);
            layoutEnumerator.MoveParent();
        }
    } while (layoutEnumerator.MoveNextLogical());
}

/// <summary>
/// Enumerate through layoutEnumerator's layout entity collection back-to-front, in a DFS manner, and in a "Logical" order.
/// </summary>
private static void TraverseLayoutBackwardLogical(LayoutEnumerator layoutEnumerator, int depth)
{
    do
    {
        PrintCurrentEntity(layoutEnumerator, depth);

        if (layoutEnumerator.MoveLastChild())
        {
            TraverseLayoutBackwardLogical(layoutEnumerator, depth + 1);
            layoutEnumerator.MoveParent();
        }
    } while (layoutEnumerator.MovePreviousLogical());
}

/// <summary>
/// Print information about layoutEnumerator's current entity to the console, indented by a number of tab characters specified by indent.
/// The rectangle that we process at the end represents the area and location thereof that the element takes up in the document.
/// </summary>
private static void PrintCurrentEntity(LayoutEnumerator layoutEnumerator, int indent)
{
    string tabs = new string('\t', indent);

    Console.WriteLine(layoutEnumerator.Kind == string.Empty
        ? $"{tabs}-> Entity type: {layoutEnumerator.Type}"
        : $"{tabs}-> Entity type & kind: {layoutEnumerator.Type}, {layoutEnumerator.Kind}");

    // Only spans can contain text
    if (layoutEnumerator.Type == LayoutEntityType.Span)
        Console.WriteLine($"{tabs}   Span contents: \"{layoutEnumerator.Text}\"");

    RectangleF leRect = layoutEnumerator.Rectangle;
    Console.WriteLine($"{tabs}   Rectangle dimensions {leRect.Width}x{leRect.Height}, X={leRect.X} Y={leRect.Y}");
    Console.WriteLine($"{tabs}   Page {layoutEnumerator.PageIndex}");
}

ExpandedSee Also