usmanazizgroupdocs
6/29/2017 - 3:59 AM

GetTextCorrdinates.cs

// For complete examples and data files, please go to https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-.NET
//Get Configurations
ViewerConfig config = Utilities.GetConfigurations();

// Create image handler 
ViewerImageHandler imageHandler = new ViewerImageHandler(config);

// Guid implies that unique document name 
string guid = DocumentName;

// Init viewer image handler
ViewerImageHandler viewerImageHandler = new ViewerImageHandler(config);

//Get document rendered as an image with text extraction enabled
ImageOptions imageOptions = new ImageOptions();
imageOptions.ExtractText = true;
List<PageImage> pages = viewerImageHandler.GetPages(guid, imageOptions);

//Get document info
DocumentInfoOptions documentInfoOptions = new DocumentInfoOptions();
documentInfoOptions.ExtractText = true;
DocumentInfoContainer documentInfoContainer = viewerImageHandler.GetDocumentInfo(guid, documentInfoOptions);

// Go through all pages
foreach (PageData pageData in documentInfoContainer.Pages)
{
    Console.WriteLine("Page number: " + pageData.Number);

    //Go through all page rows
    for (int i = 0; i < pageData.Rows.Count; i++)
    {
        RowData rowData = pageData.Rows[i];

        // Write data to console
        Console.WriteLine("Row: " + (i + 1));
        Console.WriteLine("Text: " + rowData.Text);
        Console.WriteLine("Text width: " + rowData.LineWidth);
        Console.WriteLine("Text height: " + rowData.LineHeight);
        Console.WriteLine("Distance from left: " + rowData.LineLeft);
        Console.WriteLine("Distance from top: " + rowData.LineTop);

        // Get words
        string[] words = rowData.Text.Split(' ');

        // Go through all word coordinates
        for (int j = 0; j < words.Length; j++)
        {
            int coordinateIndex = j == 0 ? 0 : j + 1;

            // Write data to console
            Console.WriteLine(string.Empty);
            Console.WriteLine("Word: '" + words[j] + "'");
            Console.WriteLine("Word distance from left: " + rowData.TextCoordinates[coordinateIndex]);
            Console.WriteLine("Word width: " + rowData.TextCoordinates[coordinateIndex + 1]);
            Console.WriteLine(string.Empty);
        }
    }
}