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.

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);
                }
            }
        }
    }
}