Get text coordinates

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Viewer MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-viewer -- dnx GroupDocs.Viewer.Mcp --yes
Folders, licensing and more →

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:

using System;
using GroupDocs.Viewer;
using GroupDocs.Viewer.Options;
using GroupDocs.Viewer.Results;
// ...

using (Viewer viewer = new Viewer("sample.docx"))
{
    // Get the file information and extract it text.
    bool extractText = true;
    ViewInfoOptions viewInfoOptions = ViewInfoOptions.ForPngView(extractText);
    ViewInfo viewInfo = viewer.GetViewInfo(viewInfoOptions);

    // Display the file information and text.
    foreach (Page page in viewInfo.Pages)
    {
        Console.WriteLine($"Page: {page.Number}");
        Console.WriteLine("Text lines/words/characters:");

        foreach (Line line in page.Lines)
        {
            Console.WriteLine(line);
            foreach (Word word in line.Words)
            {
                Console.WriteLine($"\t{word}");
                foreach (Character character in word.Characters)
                {
                    Console.WriteLine($"\t\t{character}");
                }
            }
        }
    }
}
Imports System
Imports GroupDocs.Viewer
Imports GroupDocs.Viewer.Options
Imports GroupDocs.Viewer.Results
' ...

Module Program
    Sub Main(args As String())
        Using viewer As Viewer = New Viewer("sample.docx")
            ' Get the file information and extract it text.
            Dim extractText As Boolean = True
            Dim viewInfoOptions As ViewInfoOptions = ViewInfoOptions.ForPngView(extractText)
            Dim viewInfo As ViewInfo = viewer.GetViewInfo(viewInfoOptions)
        
            ' Display the file information and text.
            For Each page As Page In viewInfo.Pages
                Console.WriteLine($"Page: {page.Number}")
                Console.WriteLine("Text lines/words/characters:")
        
                For Each line As Line In page.Lines
                    Console.WriteLine(line)
                    For Each word As Word In line.Words
                        Console.WriteLine($"	{word}")
                        For Each character As Character In word.Characters
                            Console.WriteLine($"		{character}")
                        Next
                    Next
                Next
            Next
        End Using
    End Sub
End Module

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]

Download full output