You are viewing limited content. For full access, please sign in.

Question

Question

Highlight Text From a Tiff Image

asked on February 23, 2015

Hello,

I am looking to adjust the code below (From SDK Documentation) to highlight specific text found within a particular document.  For example: The document contains a string that looks something like: Ref: 123456789  and my goal is to use an SDK Script Activity to highlight this text when found in the document.  Note: This information always exists on page 1 to make things easier.  

Your help is much appreciated!  

Thanks,

Nate

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports Laserfiche.RepositoryAccess


Namespace WorkflowActivity.Scripting.AssignTextAnnotation
    '''<summary>
    '''Provides one or more methods that can be run when the workflow scripting activity is performed.
    '''</summary>
    Public Class Script1
        Inherits RAScriptClass92
        '''<summary>
        '''This method is run when the activity is performed.
        '''</summary>
        Protected Overrides Sub Execute()

            'Document?
            If Me.BoundEntryInfo.EntryType = EntryType.Document Then

                'Get a reference to page 1
                Dim pInfo As PageInfo = Document.GetDocumentInfo(Me.BoundEntryId, Me.RASession).GetPageInfo(1)

                'Create the new highlight Annotation
                Dim hA As HighlightAnnotation = New HighlightAnnotation

                'Set the parameters of the text start/end
                hA.TextStart = 900
                hA.TextEnd = 976
                hA.LinkTextToImage(new TextLinker(pInfo.ReadTextPagePartAsWords(), pInfo.ReadLocationsPagePart()))
                hA.Color = Laserfiche.RepositoryAccess.Common.LfColor.FromAbgr(65535)

                'Add the annotation to the page
                pInfo.AddAnnotation(hA)

                hA = Nothing
                pInfo = Nothing

            End If

        End Sub
    End Class
End Namespace

 

0 0

Answer

SELECTED ANSWER
replied on February 23, 2015 Show version history

This should do what you need.  Replace the content of you Sub Execute with the following:

'Document?
If Me.BoundEntryInfo.EntryType = EntryType.Document Then
    ' Create a String object to hold page text
    Dim sPageText As String = Nothing
    ' Get PageInfo Object for page1
    Dim pInfo As PageInfo = Document.GetDocumentInfo(Me.BoundEntryId, Me.RASession).GetPageInfo(1)
    ' Load page text into stream
    Using sr As System.IO.StreamReader = pInfo.ReadTextPagePart()
        ' Copy page text stream to page text string
        sPageText = sr.ReadToEnd
    End Using
    ' Create Regular Expression to find text on page
    Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("Ref:\s*\d{5,}", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    ' Apply regular Expression to page text to find match
    Dim match As System.Text.RegularExpressions.Match = regex.Match(sPageText)
    ' Create TextRange object to hold start and end points of found text
    Dim FoundRange As TextRange = Nothing
    If match.Success Then
        ' Add the Start and End index of found text to textrange object
        FoundRange = New TextRange(match.Index, match.Length + match.Index)
    End If
    ' Only process of textrange has an end index greater than 0
    If FoundRange.EndPosition > 0 Then
        ' Create Highlight Annotation object
        Dim hA As HighlightAnnotation = New HighlightAnnotation
        ' link it to Start Index of found text
         hA.TextStart = FoundRange.StartPosition
         ' link it to End Index of found text
         hA.TextEnd = FoundRange.EndPosition
         ' Link the text highlight to the image location
         hA.LinkTextToImage(New TextLinker(pInfo.ReadTextPagePartAsWords(), pInfo.ReadLocationsPagePart()))
         ' Set highlighter color
         hA.Color = Laserfiche.RepositoryAccess.Common.LfColor.FromAbgr(65535)
         ' Add the annotation to the page
         pInfo.AddAnnotation(hA)
         ' Save Annotation change
         pInfo.Save()
    End If
End If

What I did was create a Regular expression that matches if it finds "Ref:" followed by 5 or more numbers.  It then takes the location of the match and uses that to place the highlight annotation.

1 0
replied on February 24, 2015

This worked great, Thanks Bert!

0 0
replied on February 24, 2015

Originally, I was trying to use the TextRange object to pass directly to the highlighter, but then reused your code and did not really need the TextRange object at all.  To make the code a little more efficient, you can move the highlight code up into the "If match.Success" section and get rid of the TextRange object completely.

 

' Apply regular Expression to page text to find match
Dim match As System.Text.RegularExpressions.Match = regex.Match(sPageText)
' Only process if a match is found
If match.Success Then
    ' Create Highlight Annotation object
    Dim hA As HighlightAnnotation = New HighlightAnnotation
    ' link it to Start Index of found text
    hA.TextStart = match.Index
    ' link it to End Index of found text
    hA.TextEnd = match.Length + match.Index
    ' Link the text highlight to the image location
    hA.LinkTextToImage(New TextLinker(pInfo.ReadTextPagePartAsWords(), pInfo.ReadLocationsPagePart()))
    ' Set highlighter color
    hA.Color = Laserfiche.RepositoryAccess.Common.LfColor.FromAbgr(65535)
    ' Add the annotation to the page 
    pInfo.AddAnnotation(hA)
    ' Save Annotation change
    pInfo.Save()
End If

 

1 0
replied on April 20, 2015

What would the code look like to parse ALL pages in a document for the text that we want to highlight?

0 0
replied on April 20, 2015

Rather than just grabbing the first page pageinfo, you need to get all pageinfo objects and then loop through them.

            'Document?
            If Me.BoundEntryInfo.EntryType = EntryType.Document Then
                ' Create a String object to hold page text
                Dim sPageText As String = Nothing
                ' Get All PageInfo Object from document
                Using PageReader as PageInfoReader = Document.GetDocumentInfo(Me.BoundEntryId, Me.RASession).GetPageInfos()
                    For Each pInfo As PageInfo In PageReader
                        ' Ensure sPageText object is nothing
                        sPageText = Nothing
                        ' Load page text into stream
                        Using sr As System.IO.StreamReader = pInfo.ReadTextPagePart()
                            ' Copy page text stream to page text string
                            sPageText = sr.ReadToEnd
                        End Using
                        ' Create Regular Expression to find text on page
                        Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("Ref:\s*\d{5,}", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                        ' Apply regular Expression to page text to find match
                        Dim match As System.Text.RegularExpressions.Match = regex.Match(sPageText)
                        ' Only process if a match is found
                        If match.Success Then
                            ' Create Highlight Annotation object
                            Dim hA As HighlightAnnotation = New HighlightAnnotation
                            ' link it to Start Index of found text
                            hA.TextStart = match.Index
                            ' link it to End Index of found text
                            hA.TextEnd = match.Length + match.Index
                            ' Link the text highlight to the image location
                            hA.LinkTextToImage(New TextLinker(pInfo.ReadTextPagePartAsWords(), pInfo.ReadLocationsPagePart()))
                            ' Set highlighter color
                            hA.Color = Laserfiche.RepositoryAccess.Common.LfColor.FromAbgr(65535)
                            ' Add the annotation to the page
                            pInfo.AddAnnotation(hA)
                            ' Save Annotation change
                            pInfo.Save()
                        End If
                    Next              
                End Using
            End If

 

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.