LayoutCollector Class

This class allows to compute page numbers of document nodes.

Inheritance Hierarchy
SystemObject
  Aspose.Words.LayoutLayoutCollector

Namespace:  Aspose.Words.Layout
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class LayoutCollector

The LayoutCollector type exposes the following members.

Constructors
  NameDescription
Public methodCode exampleLayoutCollector
Initializes an instance of this class.
Properties
  NameDescription
Public propertyCode exampleDocument
Gets or sets the document this collector instance is attached to.
Methods
  NameDescription
Public methodCode exampleClear
Clears all collected layout data. Call this method after document was manually updated, or layout was rebuilt.
Public methodEquals (Inherited from Object.)
Public methodCode exampleGetEndPageIndex
Gets 1-based index of the page where node ends. Returns 0 if node cannot be mapped to a page.
Public methodCode exampleGetEntity
Returns an opaque position of the LayoutEnumerator which corresponds to the specified node. You can use returned value as an argument to Current given the document being enumerated and the document of the node are the same.
Public methodGetHashCode (Inherited from Object.)
Public methodCode exampleGetNumPagesSpanned
Gets number of pages the specified node spans. 0 if node is within a single page. This is the same as GetEndPageIndex(Node) - GetStartPageIndex(Node).
Public methodCode exampleGetStartPageIndex
Gets 1-based index of the page where node begins. Returns 0 if node cannot be mapped to a page.
Public methodGetType (Inherited from Object.)
Public methodToString (Inherited from Object.)
Remarks

When you create a LayoutCollector and specify a Document document object to attach to, the collector will record mapping of document nodes to layout objects when the document is formatted into pages.

You will be able to find out on which page a particular document node (e.g. run, paragraph or table cell) is located by using the GetStartPageIndex(Node), GetEndPageIndex(Node) and GetNumPagesSpanned(Node) methods. These methods automatically build page layout model of the document and update fields if required.

When you no longer need to collect layout information, it is best to set the Document property to null to avoid unnecessary collection of more layout mappings.

Examples
Shows how to see the page spans of nodes.
// Open a blank document and create a DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Create a LayoutCollector object for our document that will have information about the nodes we placed
LayoutCollector layoutCollector = new LayoutCollector(doc);

// The document itself is a node that contains everything, which currently spans 0 pages
Assert.AreEqual(doc, layoutCollector.Document);
Assert.AreEqual(0, layoutCollector.GetNumPagesSpanned(doc));

// Populate the document with sections and page breaks
builder.Write("Section 1");
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
doc.AppendChild(new Section(doc));
doc.LastSection.AppendChild(new Body(doc));
builder.MoveToDocumentEnd();
builder.Write("Section 2");
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);

// The collected layout data won't automatically keep up with the real document contents
Assert.AreEqual(0, layoutCollector.GetNumPagesSpanned(doc));

// After we clear the layout collection and update it, the layout entity collection will be populated with up-to-date information about our nodes
// The page span for the document now shows 5, which is what we would expect after placing 4 page breaks
layoutCollector.Clear();
doc.UpdatePageLayout();
Assert.AreEqual(5, layoutCollector.GetNumPagesSpanned(doc));

// We can also see the start/end pages of any other node, and their overall page spans
NodeCollection nodes = doc.GetChildNodes(NodeType.Any, true);
foreach (Node node in nodes)
{
    Console.WriteLine($"->  NodeType.{node.NodeType}: ");
    Console.WriteLine(
        $"\tStarts on page {layoutCollector.GetStartPageIndex(node)}, ends on page {layoutCollector.GetEndPageIndex(node)}," +
        $" spanning {layoutCollector.GetNumPagesSpanned(node)} pages.");
}

// We can iterate over the layout entities using a LayoutEnumerator
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
Assert.AreEqual(LayoutEntityType.Page, layoutEnumerator.Type);

// The LayoutEnumerator can traverse the collection of layout entities like a tree
// We can also point it to any node's corresponding layout entity like this
layoutEnumerator.Current = layoutCollector.GetEntity(doc.GetChild(NodeType.Paragraph, 1, true));
Assert.AreEqual(LayoutEntityType.Span, layoutEnumerator.Type);
Assert.AreEqual("¶", layoutEnumerator.Text);
See Also