搜索并突出显示PDF格式的文本 [英] Search and Highlight Text in PDF

查看:211
本文介绍了搜索并突出显示PDF格式的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想询问是否可以使用C#搜索和突出显示PDF文件中的多个文本。



非常感谢您的建议。谢谢。

解决方案

下面是一个示例VB.NET代码,用于突出显示PDF格式的标记文本:



 私有  Sub  TextMarkButton1_Click()
' ~~>将其更改为您的文件名
filey = c:\test.pdf
设置 AcroExchApp = CreateObject( AcroExch.App
设置 AcroExchAVDoc = CreateObject( AcroExch.AVDoc

' ~~>打开pdf文件
AcroExchAVDoc.Open filey,

' ~~>获取与打开的AVDoc相关联的PDDoc
设置 AcroExchPDDoc = AcroExchAVDoc.GetPDDoc

' ~~>搜索文本
sustext = 发布

' ~~>获取JavaScript对象
设置 jso = AcroExchPDDoc.GetJSObject

' ~~>显示应用程序
AcroExchApp.Show

' ~~>找到的匹配数
nCount = 0

如果 jso 没什么 然后
' ~~>总页数pdf
nPages = jso.numpages
' ~~ >遍历页面
对于 i = 0 nPages - 1
' ~~>获取单词计数在一个页面
nWords = jso.getPageNumWords(i)
' ~~>在该页面中循环通过单词
对于 j = 0 nWords - 1
' ~~>获取每个单词
word = Trim( CStr (jso.getPageNthWord(i,j)))
如果 word<> 然后
< span class =code-comment>' ~~>匹配单词与搜索文本
result = StrComp(word,sustext,vbTextCompare)
' ~~如果找到,增量计数
如果结果= 0 < span class =code-keyword>然后 nCount = nCount + 1
结束 如果
下一步 j
下一步 i
结束 如果

< span class =code-comment>' ~~>显示找到的匹配项数
MsgBox nCount

' ~~ >清理
设置 jso = 没什么
设置 AcroExchAVDoc =
设置 AcroExchApp = < span class =code-keyword> Nothing
结束 Sub


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.

解决方案

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
    AcroExchApp.Show

    '~~> 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


这篇关于搜索并突出显示PDF格式的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆