LayoutCollector class

This class allows to compute page numbers of document nodes.

To learn more, visit the Converting to Fixed-page Format documentation article.

public class LayoutCollector


LayoutCollector(Document)Initializes an instance of this class.


Document { get; set; }Gets or sets the document this collector instance is attached to.


Clear()Clears all collected layout data. Call this method after document was manually updated, or layout was rebuilt.
GetEndPageIndex(Node)Gets 1-based index of the page where node ends. Returns 0 if node cannot be mapped to a page.
GetEntity(Node)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.
GetNumPagesSpanned(Node)Gets number of pages the specified node spans. 0 if node is within a single page. This is the same as GetEndPageIndex - GetStartPageIndex.
GetStartPageIndex(Node)Gets 1-based index of the page where node begins. Returns 0 if node cannot be mapped to a page.


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, GetEndPageIndex and GetNumPagesSpanned 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.


Shows how to see the the ranges of pages that a node spans.

Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);

// Call the "GetNumPagesSpanned" method to count how many pages the content of our document spans.
// Since the document is empty, that number of pages is currently zero.
Assert.AreEqual(doc, layoutCollector.Document);
Assert.AreEqual(0, layoutCollector.GetNumPagesSpanned(doc));

// Populate the document with 5 pages of content.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Section 1");
builder.Write("Section 2");

// Before the layout collector, we need to call the "UpdatePageLayout" method to give us
// an accurate figure for any layout-related metric, such as the page count.
Assert.AreEqual(0, layoutCollector.GetNumPagesSpanned(doc));


Assert.AreEqual(5, layoutCollector.GetNumPagesSpanned(doc));

// We can see the numbers of the start and end pages of any node and their overall page spans.
NodeCollection nodes = doc.GetChildNodes(NodeType.Any, true);
foreach (Node node in nodes)
    Console.WriteLine($"->  NodeType.{node.NodeType}: ");
        $"\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 apply it to any node's corresponding layout entity.
layoutEnumerator.Current = layoutCollector.GetEntity(doc.GetChild(NodeType.Paragraph, 1, true));

Assert.AreEqual(LayoutEntityType.Span, layoutEnumerator.Type);
Assert.AreEqual("¶", layoutEnumerator.Text);

See Also