LayoutCollectorGetEntity Method |
Namespace: Aspose.Words.Layout
This method works for only Paragraph nodes, as well as indivisible inline nodes, e.g. BookmarkStart or Shape. It doesn't work for Run, CellRow or Table nodes, and nodes within header/footer.
Note that the entity returned for a Paragraph node is a paragraph break span. Use the appropriate method to ascend to the parent line
If you need to navigate to a Run of text then you can insert bookmark right before it and then navigate to the bookmark instead.
If you need to navigate to a Cell node then you can move to a Paragraph node in this cell and then ascend to a parent entity. The same approach can be used for Row and Table 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);