Free Support Forum - aspose.com

Aspose LayoutController get entity is returning null

Aspose layout controllers get entity is returning null for SDT nodes and we are not able to track why? See the attached document.

SimpleINDoc.zip (551.5 KB)

Also when we are trying to assign bookmark for the same document is it is giving error for SDT named “Hypothesis”.
Please check the concern.

Document LobjDocument = new Document(PsDocument);
string pdfDocument = PsDocument;
pdfDocument = pdfDocument.Replace(".docx", “.pdf”);
DocumentBuilder builder = new DocumentBuilder(LobjDocument);
NodeCollection nodes = LobjDocument.GetChildNodes(NodeType.StructuredDocumentTag, true);
int iCount = nodes.Count;
LayoutCollector objLayoutCollector = new LayoutCollector(LobjDocument);
LayoutEnumerator objLayoutEnumerator = new LayoutEnumerator(LobjDocument);
foreach (StructuredDocumentTag sdt in nodes)
{
Object obk = objLayoutCollector.GetEntity(sdt);
if(obk != null)
{
objLayoutEnumerator.Current = obk;
Debug.WriteLine("Bookmark : " + sdt.Tag);
builder.MoveTo(sdt);
BookmarkStart start = builder.StartBookmark(sdt.Tag);
BookmarkEnd end = builder.EndBookmark(sdt.Tag);
Node first = sdt.FirstChild;
Node last = sdt.LastChild;
// first.InsertBefore(start, first.FirstChild);
// last.InsertAfter(end, last.LastChild);
sdt.ParentNode.InsertBefore(start, sdt);
sdt.ParentNode.InsertAfter(end, sdt);
//RectangleF rectF = objLayoutEnumerator.Rectangle;
//Rectangle rect = new Rectangle(PointToPixel(rectF.Left, g.DpiX), PointToPixel(rectF.Top, g.DpiY), PointToPixel(rectF.Width, g.DpiX), PointToPixel(rectF.Height, g.DpiY));
//Debug.WriteLine("Rectangle pos : X " + rectF.X + " Y " + rectF.Y + " width " + rectF.Width + " height " + rectF.Height);
//Debug.WriteLine(sdt.Range.Text);

                    }
                }
              PdfSaveOptions options = new PdfSaveOptions();
              options.OutlineOptions.DefaultBookmarksOutlineLevel = 9;
              LobjDocument.UpdateFields();
              LobjDocument.Save(pdfDocument, options);
            }

@mhtsharma9,

Thanks for your inquiry. Please note that LayoutCollector.GetEntity method works for only Paragraph nodes, as well as indivisible inline nodes, e.g. BookmarkStart or Shape.

In your case, we suggest you please insert a bookmark in the content control and use BookmarkStart node in this method. Hope this helps you.

Is this the limitation of Aspose Word? Because Microsoft Word is allowing me to insert the bookmark in the same node.
Also can you please provide code snippet for workaround provided above?

@mhtsharma9,

Thanks for your inquiry. Please use the following code example to get the position of text in the content control using layout API.

We have noticed that the bookmarks are not inserted into first and second content control of body of document. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16948. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Document LobjDocument = new Document(MyDir + "SimpleINDoc.docx");

DocumentBuilder builder = new DocumentBuilder(LobjDocument);
NodeCollection nodes = LobjDocument.FirstSection.Body.GetChildNodes(NodeType.StructuredDocumentTag, true);
int iCount = nodes.Count;
int i = 1;
LayoutCollector objLayoutCollector = new LayoutCollector(LobjDocument);
LayoutEnumerator objLayoutEnumerator = new LayoutEnumerator(LobjDocument);
foreach (StructuredDocumentTag sdt in nodes)
{
    if (sdt.FirstChild != null) 
    {
        if (sdt.FirstChild.NodeType == NodeType.Paragraph)
        {
            Object obk = objLayoutCollector.GetEntity(sdt.FirstChild);
            if (obk != null)
            {
                objLayoutEnumerator.Current = obk;
                RectangleF rectF = objLayoutEnumerator.Rectangle;
                Console.WriteLine(rectF);

                builder.MoveTo(sdt.FirstChild);
                BookmarkStart start = builder.StartBookmark("bookmark" + i);
                BookmarkEnd end = builder.EndBookmark("bookmark" + i);
                i++;
            }
        }
        else if (sdt.FirstChild.NodeType == NodeType.Run)
        {
            builder.MoveTo(sdt.FirstChild);
            BookmarkStart start = builder.StartBookmark("bookmark" + i);
            BookmarkEnd end = builder.EndBookmark("bookmark" + i);
            i++;
            Object obk = objLayoutCollector.GetEntity(start);
            if (obk != null)
            {
                objLayoutEnumerator.Current = obk;
                RectangleF rectF = objLayoutEnumerator.Rectangle;
                Console.WriteLine(rectF);
            }
        }
    }

}