GroupDocsGists
10/19/2017 - 11:20 AM

GetTextCoordinatesInImage.vb

' For complete examples and data files, please go to https://github.com/groupdocs-annotation/GroupDocs.Annotation-for-.NET
''' <summary>
''' Gets text coordinates in image representation of document
''' </summary>
''' <param name="filePath">Source file path</param> 
Public Shared Sub GetTextCoordinates(filePath As String)
    Try
        ' Set configuration
        Dim cfg As AnnotationConfig = GetConfiguration()

        ' Initialize annotator 
        Dim annotator As New AnnotationImageHandler(cfg)
        Try
            annotator.CreateDocument(filePath)
        Catch
        End Try

        Dim documentInfoContainer = annotator.GetDocumentInfo(filePath)

        ' Go through all pages
        For Each pageData As PageData In documentInfoContainer.Pages
            Console.WriteLine("Page number: " + pageData.Number)

            'Go through all page rows
            For i As Integer = 0 To pageData.Rows.Count - 1
                Dim rowData As 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
                Dim words As String() = rowData.Text.Split(" "c)

                ' Go through all word coordinates
                For j As Integer = 0 To words.Length - 1
                    Dim coordinateIndex As Integer = If(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)
                Next
                Console.ReadKey()
            Next
        Next
    Catch exp As System.Exception
        Console.WriteLine(exp.Message)
    End Try

End Sub