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)