We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to know position of text in msword using Aspose words?


Coudl you please let me know if I can postion of text in MS Word using aspose words in java which should be similar to bounding boxes in pdf.

I have an use case where in have to extract text in msword pertaining to specific location in pixels.

Can you please suggest me, how can I do it using Aspose words in Java



Thanks for your inquiry. The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages. Please check the members of LayoutCollector and LayoutEnumerator.

LayoutCollector.GetEntity method returns an opaque position of the LayoutEnumerator which corresponds to the specified node. 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.

All text of the document is stored in runs of text. In your case, we suggest you following solution.

  1. Find the text and insert the bookmark before the text. You need to implement IReplacingCallback interface to find the text and insert bookmark.

  2. Please use the Aspose.Words.Layout API to get the position of bookmark as shown below.

    Document doc = new Document(MyDir + “input.docx”);

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

    Bookmark bookmark = doc.getRange().getBookmarks().get(0);
    Object renderObject = collector.getEntity(bookmark.getBookmarkStart());

    System.out.println("X : " + layoutEnumerator.getRectangle().getX());
    System.out.println("Y : " + layoutEnumerator.getRectangle().getY());