Use the ExtractText property 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:
usingSystem;usingGroupDocs.Viewer;usingGroupDocs.Viewer.Options;usingGroupDocs.Viewer.Results;// ...using(Viewerviewer=newViewer("sample.docx")){// Get the file information and extract it text.boolextractText=true;ViewInfoOptionsviewInfoOptions=ViewInfoOptions.ForPngView(extractText);ViewInfoviewInfo=viewer.GetViewInfo(viewInfoOptions);// Display the file information and text.foreach(PagepageinviewInfo.Pages){Console.WriteLine($"Page: {page.Number}");Console.WriteLine("Text lines/words/characters:");foreach(Linelineinpage.Lines){Console.WriteLine(line);foreach(Wordwordinline.Words){Console.WriteLine($"\t{word}");foreach(Charactercharacterinword.Characters){Console.WriteLine($"\t\t{character}");}}}}}
ImportsSystemImportsGroupDocs.ViewerImportsGroupDocs.Viewer.OptionsImportsGroupDocs.Viewer.Results' ...
ModuleProgramSubMain(argsAsString())UsingviewerAsViewer=NewViewer("sample.docx")' Get the file information and extract it text.
DimextractTextAsBoolean=TrueDimviewInfoOptionsAsViewInfoOptions=ViewInfoOptions.ForPngView(extractText)DimviewInfoAsViewInfo=viewer.GetViewInfo(viewInfoOptions)' Display the file information and text.
ForEachpageAsPageInviewInfo.PagesConsole.WriteLine($"Page: {page.Number}")Console.WriteLine("Text lines/words/characters:")ForEachlineAsLineInpage.LinesConsole.WriteLine(line)ForEachwordAsWordInline.WordsConsole.WriteLine($" {word}")ForEachcharacterAsCharacterInword.CharactersConsole.WriteLine($" {character}")NextNextNextNextEndUsingEndSubEndModule
sample.docx is the sample file used in this example. Click here to download it.
Page: 1
Text lines/words/characters:
Line 'Harbor Lane Provisions - Annual Report 2025 | Page 1' X=341.103 Y=803.10099 198.2x8.8px with 10 word(s).
Word 'Harbor' X=341.103 Y=803.10099 24.456x8.8px with 6 character(s).
Character 'H' X=341.103 Y=803.101 5.776x8.8px.
Character 'a' X=346.879 Y=803.101 4.448x8.8px.
Character 'r' X=351.327 Y=803.101 2.664x8.8px.
Character 'b' X=353.991 Y=803.101 4.448x8.8px.
Character 'o' X=358.439 Y=803.101 4.448x8.8px.
Character 'r' X=362.895 Y=803.101
[TRUNCATED]