Get text coordinates
Leave feedback
Use the setExtractText() method of the ViewInfoOptions class to get the text contained in a source document and its coordinates. Then you can use this data to add a selectable text over the image or to implement a text search in image-based rendering.
The following code snippet shows how to retrieve and print out text (lines/words/characters) on each document page with coordinates:
import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.ViewInfoOptions;
import com.groupdocs.viewer.results.Character;
import com.groupdocs.viewer.results.Line;
import com.groupdocs.viewer.results.Page;
import com.groupdocs.viewer.results.ViewInfo;
import com.groupdocs.viewer.results.Word;
// ...
try (Viewer viewer = new Viewer("sample.docx")) {
ViewInfoOptions viewInfoOptions = ViewInfoOptions.forPngView(true);
ViewInfo viewInfo = viewer.getViewInfo(viewInfoOptions);
for (Page page : viewInfo.getPages()) {
System.out.println("Page: " + page.getNumber());
System.out.println("Text lines/words/characters:");
for (Line line : page.getLines()) {
System.out.println(line);
for (Word word : line.getWords()) {
System.out.println("\t" + word);
for (Character character : word.getCharacters()) {
System.out.println("\t\t" + character);
}
}
}
}
}
The following image shows a sample console output:
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.