Getting Null error in LayoutCollector.GetEntity() Function

Hii team,
i am trying to get height and width of content control using LayoutCollector and LayoutEnumerator and adding this height, width into bookmark name with tag name.
Getting null issue in this line : LayoutEnumerator.Current = LayoutCollector.GetEntity(((Paragraph)sdt.ChildNodes.OfType<Paragraph>().LastOrDefault()).ChildNodes.LastOrDefault());

Please provide any solution for this issue , And provide any reason for why null getting in this line.

Snippet :

Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\\InputDoc.docx");
try
{
    var sdts = outputDoc.GetChildNodes(NodeType.StructuredDocumentTag, true).Where(x => ((Aspose.Words.Markup.StructuredDocumentTag)x).Tag.StartsWith("Rangescop_")
                || ((Aspose.Words.Markup.StructuredDocumentTag)x).Tag.StartsWith("Foot_") || ((Aspose.Words.Markup.StructuredDocumentTag)x).Tag.StartsWith("Com_"));
    DocumentBuilder Builder = new DocumentBuilder(outputDoc);
    foreach (Aspose.Words.Markup.StructuredDocumentTag sdt in sdts)
    {
        var innerSdtTag = ((Aspose.Words.CompositeNode)sdt).ChildNodes.Where(x => x.NodeType == NodeType.StructuredDocumentTag).FirstOrDefault();
        var innerPnodes = ((Aspose.Words.CompositeNode)sdt).ChildNodes.Where(x => x.NodeType == NodeType.Paragraph);
        if (innerPnodes.Count() > 1 || (innerPnodes.Count() > 0 && innerSdtTag != null && ((Aspose.Words.CompositeNode)innerSdtTag).ChildNodes != null) && ((Aspose.Words.CompositeNode)innerSdtTag).ChildNodes.Where(x => x.NodeType == NodeType.Paragraph).FirstOrDefault() != null)
        {
            LayoutCollector LayoutCollector = new LayoutCollector(outputDoc);
            LayoutEnumerator LayoutEnumerator = new LayoutEnumerator(outputDoc)
            {
                Current = LayoutCollector.GetEntity(((Paragraph)sdt.ChildNodes.OfType<Paragraph>().FirstOrDefault()).ChildNodes.FirstOrDefault())
            };
            while (LayoutEnumerator.Type != LayoutEntityType.Line)
                LayoutEnumerator.MoveParent();

            //Get the rectangle occuped by the first line of the paragraph.
            RectangleF rect1 = LayoutEnumerator.Rectangle;
            //Now do the same woth the last line.
            LayoutEnumerator.Current = LayoutCollector.GetEntity(((Paragraph)sdt.ChildNodes.OfType<Paragraph>().LastOrDefault()).ChildNodes.LastOrDefault());
            while (LayoutEnumerator.Type != LayoutEntityType.Line)
                LayoutEnumerator.MoveParent();

            RectangleF rect2 = LayoutEnumerator.Rectangle;
            //Union of the rectangles is the region occuped by the paragraph.
            RectangleF result = RectangleF.Union(rect1, rect2);
            Builder.MoveTo(((Paragraph)sdt.ChildNodes.OfType<Paragraph>().FirstOrDefault()).ChildNodes.OfType<Run>().FirstOrDefault());
            string temp = sdt.Tag.ToString() + "_" + result.Height.ToString() + "_" + result.Width.ToString();
            Builder.StartBookmark(temp);
            Builder.EndBookmark(temp);
            LayoutCollector.Clear();
        }
    }
    outputDoc.UpdatePageLayout();

}
catch (Exception e)
{
    throw e;
}
doc.Save(@"C:\\OutputDoc.docx", SaveFormat.Docx);

InputDoc.docx (2.6 MB)

@AlpeshChaudhari12345 The problem in in the following line of code:

Current = LayoutCollector.GetEntity(((Paragraph)sdt.ChildNodes.OfType<Paragraph>().FirstOrDefault()).ChildNodes.FirstOrDefault())

Here you are trying to get entity for a Run node, but LayoutCollector.GetEntity works for only Paragraph nodes, as well as indivisible inline nodes, e.g. BookmarkStart or Shape. Please see the documentation for more information:
https://reference.aspose.com/words/net/aspose.words.layout/layoutcollector/getentity/
You should change the code like this:

Current = LayoutCollector.GetEntity(sdt.ChildNodes.OfType<Paragraph>().FirstOrDefault())

and later in your code:

//Now do the same woth the last line.
LayoutEnumerator.Current = LayoutCollector.GetEntity(sdt.ChildNodes.OfType<Paragraph>().LastOrDefault());

@alexey.noskov thanks for response…

1 Like