How to get Height of First Run Node using .NET | Paragraph Height

I want to figure out the bounding box of paragraphs (x, y, weight and height). I find that I can use LayoutCollector and LayoutEnumerator to get the paragraph’s rectangle data. However the enumerator will point to the end of the paragraph. How I can know the position of the start of the paragraph so I can calculate the height?

@adayao

You can insert the bookmark at the start of paragraph and get the height of paragraph using layout APIs. Please check the following code example.

Document doc = new Document(MyDir + "bookmark.docx");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    var renderObject = collector.GetEntity(bookmark.BookmarkStart);
    layoutEnumerator.Current = renderObject;
    Console.WriteLine(layoutEnumerator.Rectangle.Height);
}

How can I insert bookmark? Is it able to do that with DocumentBuilder? And if it’s convenient for you, would you mind offering Java code examples?

@adayao

Please move the cursor to the paragraph and insert bookmark. We suggest you please read the following articles.

Following code example shows how to insert bookmark at the start of paragraph and get its height.

Document doc = new Document(MyDir + "in.docx");
int i =1;

NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
for(Paragraph paragraph :(Iterable<Paragraph>)paragraphs)
{
	DocumentBuilder builder = new DocumentBuilder(doc);
	builder.moveToParagraph(paragraphs.indexOf(paragraph), 0);
	builder.startBookmark("bookmark"+i);
	builder.endBookmark("bookmark"+i);
	i++;
}

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
for (Bookmark bookmark : doc.getRange().getBookmarks())
{
    Object renderObject = collector.getEntity(bookmark.getBookmarkStart());
    layoutEnumerator.setCurrent(renderObject);
    System.out.println(layoutEnumerator.getRectangle().getHeight());
}