Cast com.aspose.pdf.font to com.aspose.words.font using Java

how to get a instance of com.aspose.pdf.font from a instance of com.aspose.words.font

@liu9527

Please note that Aspose.Words and Aspose.PDF are two different products. So, com.aspose.pdf.Font cannot cast into com.aspose.words.Font. You can read the properties of com.aspose.words.Font and use them according to requirement.

i need to measure width of text when using aspose.word,com.aspose.words.font don’t have method of measureString,but com.aspose.pdf.font support this

@liu9527

Aspose.Words does not provide such property to get the width of string. However, you can achieve your requirement using Layout API of Aspose.Words. 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.

In your case, we suggest you following solution. Hope this helps you.

  1. Find the text and bookmark it. The text will be looked like [BookmarkStart]Text[BookmarkEnd] in Aspose.Words’ DOM.
  2. Get the position of BookmarkStart and BookmarkEnd nodes.
  3. Get the difference of both nodes for width of text.

Following code example shows how to get the position of BookmarkStart node.

Document doc = new Document(MyDir + "input.docx");
Bookmark bookmark = doc.getRange().getBookmarks().get("bookmarkname");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

Object renderObject = collector.getEntity(bookmark.getBookmarkStart());
layoutEnumerator.setCurrent(renderObject);
System.out.println(layoutEnumerator.getRectangle().getX());