Click here to Skip to main content
15,920,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I would just like to ask if it is possible to search and highlight multiple text in a PDF file using C#.

Your advice is very much appreciated. Thank you.

1 solution

Here is a sample VB.NET code to highlight the marked text in PDF:

Private Sub TextMarkButton1_Click()
    '~~> Change this to your File Name
    filey = "c:\test.pdf"
    Set AcroExchApp = CreateObject("AcroExch.App")
    Set AcroExchAVDoc = CreateObject("AcroExch.AVDoc")

    '~~> Open the pdf file
    AcroExchAVDoc.Open filey, ""

    '~~> Get the PDDoc associated with the open AVDoc
    Set AcroExchPDDoc = AcroExchAVDoc.GetPDDoc

    '~~> Search Text
    sustext = "Release"

    '~~> get JavaScript Object
    Set jso = AcroExchPDDoc.GetJSObject

    '~~> Show application

    '~~> Count of Matches Found
    nCount = 0

    If Not jso Is Nothing Then
        '~~> Total No of pages in pdf
        nPages = jso.numpages
        '~~> Loop through pages
        For i = 0 To nPages - 1
            '~~> Get words Count in one Page
            nWords = jso.getPageNumWords(i)
            '~~> Loop Thru words in that Page
            For j = 0 To nWords - 1
                '~~> Get each word
                word = Trim(CStr(jso.getPageNthWord(i, j)))
                If word <> "" Then
                    '~~> Match word with Search text
                    result = StrComp(word, sustext, vbTextCompare)
                    '~~ if found, increment count
                    If result = 0 Then nCount = nCount + 1
                End If
            Next j
        Next i
    End If

    '~~> Display Number of matches found
    MsgBox nCount

    '~~> Clean Up
    Set jso = Nothing
    Set AcroExchAVDoc = Nothing
    Set AcroExchApp = Nothing
End Sub
Share this answer
akosidab 14-Jun-12 21:39pm    

Can you please post the whole code? Coz I don't know the objects you used it causes a lot of errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900