Image Viewer - Get text coordinates

GroupDocs.Viewer provides the feature of getting text coordinates. This feature is useful if you want to add selectable text over the image or implement a text search in image-based rendering. 

The isExtractText() property of ViewInfoOptions class enables you to get the text contained in a source document with coordinates. To change the property you have to create class ViewInfoOptions using static method forJpgView(…) or forPngView(…) with appropriate parameter (true or false).

Following code sample shows how to retrieve and print out text (lines / words / characters) of each document page with coordinates.

    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);
                }
            }
        }
    }
    viewer.close();

More resources

Advanced Usage Topics

To learn more about document viewing features, please refer to the advanced usage section.

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.