如何使用Excel VBA搜索和突出显示PDF中的文本

问题描述 投票:0回答:3

是的,所以经过几个小时的搜索;我没有为excel vba提供任何东西,我觉得这很令人惊讶。找到了一些我试图移植但没有运气的vbs。我已经设法将pdf文本导入到工作表中并进行搜索,这很好;但这显然不允许我实际突出显示pdf。

我要做的是打开PDF文档,搜索关键字,然后突出显示这些单词并保存。我有adobe acrobat X,所以必须有某种API才能让我用excel vba做到这一点?我将不得不使用像iText这样的开源库;我不愿意。

我看到的一些vbs涉及逐字母查找文本,然后在它周围绘制矩形并用javascript着色,这看起来似乎不必要地复杂(无论如何都无法使端口工作......)。

澄清:我不想突出显示excel中的文本,我想在PDF上突出显示它。我只是在Excel中读取它来搜索文本并查看它是否在PDF中,因为我不知道如何做到这一点。

PS:能够在图像pdf上使用OCR也很好。

vba pdf com highlight acrobat
3个回答
0
投票

Excel无法清晰地打开pdf文件格式。

为了做你想做的事,你需要某种PDF转换器来将文档翻译成Excel可以读取的格式(例如xls或txt)。然后,您可以使用normal.Find和.Format方法来完成您的任务。

以下是我从快速Google搜索中找到的一些免费转换器(虽然请注意我没有使用过任何这些,因此您可能希望进行其他研究)

http://www.freepdfconvert.com/‎
http://www.pdf995.com/download.html
http://www.cutepdf.com/
http://www.primopdf.com/
http://sourceforge.net/projects/pdfcreat... 

但是请注意,以打开它们的格式保存它们很可能是不可能的。这完全取决于转换器的好坏程度。最终,我认为Excel不是您想要用于此任务的工具。


0
投票

远程控制Acrobat有一些可能性。在Mac上,它是通过AppleScript,在Windows上,它是通过VB / VBS(如果我没记错的话)。无论如何,您可以运行Acrobat JavaScript。

您可以从Adobe网站下载Acrobat SDK,并查看Documentation文件夹。

尽管没有那么好的经验,但这是一种方法:循环遍历文档的所有页面,遍历实际页面上的所有“单词”,读出找到的单词的边界框的坐标(也被称为“四边形”,可能与其他“单词”进行一些比较,以确定这些“单词”是否属于一起。最后使用读出四边形作为坐标创建一个Highlight Annotation。

在PDF文档中查找单词的另一种可能性是使用Redaction工具的标记部分(在删除和写回编校文档之前停止编校过程)。然后,您将运行一个枚举所有Redaction类型注释的Acrobat JavaScript,并用类似的Highlight注释替换它们。


0
投票

好吧,玩了一下我已经拥有的代码和js annots。附上你会发现一个VBScript可以标记/突出显示一个永久的单词。它可以很容易地更改为仅标记一个单词。在AcroJS帮助文件中,您可以找到标记装备的一些选项。

VBS代码我写的VBA就像。因此,您可以将其直接复制到IDE中。

享受,莱因哈德

 '// Save this as xxx.vbs and start with Double Click
 '// Acrobat must be opend before with an active document!! -otherwise error-

wordTF = "Reinhard" '//word to find
pdfText = ""

set WshShell = CreateObject ("Wscript.Shell")
WshShell.AppActivate("Adobe Acrobat")
WScript.Sleep 500

'// get the active Document
Set AcroApp = CreateObject("AcroExch.App")
Set AVDoc = AcroApp.GetActiveDoc
Set PDDoc = AVDoc.GetPDDoc
Set AForm = CreateObject("AFormAut.App") 'connect to Form API for later use

maxPages = PdDoc.GetNumPages
for p = 0 to maxPages - 1  '// start the page loop
    Set PdfPage = PDDoc.AcquirePage(p) '// p  = Pagenumber (zero based)
    Set PageHL = CreateObject("AcroExch.HiliteList")  '// created to get the page text
    PageHLRes = PageHL.Add(0,9000) '<<--SET in FILE! (Start,END[9000=All])
    Set PageSel = PdfPage.CreatePageHilite(PageHL)

    for i = 0 to PageSel.Getnumtext - 1   '// start the word loop on current page
        word = PageSel.getText(i)         '// get one word
        pdfText = pdfText & word          '// gather words on page

        if instr(word, wordTF) then       '// used instr because the "word" you may get as "word "
            msgbox("add:""" &word &"""")                Set wordToHl = CreateObject("AcroExch.HiliteList") '// created to get the word on list
            wordToHl.Add i, 1 'Hilite the word Reinhard
            Set wordHl = PdfPage.CreateWordHilite(wordToHl)
            Set rect = wordHl.GetBoundingRect
            msgbox("left:" &rect.Left &" bot:" &rect.bottom &" right:"&rect.Right &" top:" &rect.Top)
            AVDoc.SetTextSelection(wordHl)  '// highlight the word (not really needed)
            AVDoc.ShowTextSelect()          '// show highlighted text (not really needed)
              '// write and execute js to mark permanent (to lazy to translate to jso)
            ex = " // set annot for text selection " &vbLf _
                & "var sqannot = this.addAnnot({type: ""Square"", page: 1, " &vbLf _
                & "rect: [" &rect.left &", "& rect.top &", " &rect.right &", " &rect.bottom &"], " &vbLf _
                & "name: ""p" &p &"i" &i &"""});"
            msgbox(ex)
            AForm.Fields.ExecuteThisJavaScript ex
        end if '// word found
    Next  '// get next word
    msgBox(pdfText)
    pdfText = ""
next '// get next page
msgbox("Done!")
© www.soinside.com 2019 - 2024. All rights reserved.