VB.Net VBA 访问 Acrobat,浏览所有 annotation. 代码执行通过.

下面是VBA中的代码

'//mr:: 调用acrobat的方法, 大小写都可以.
'//mr:: 不需要AcroExch.App这个对象.
Sub GetAnnotationFromPDF()

Dim AcrApp As Acrobat.AcroApp

Dim AcrAvDoc As Acrobat.AcroAVDoc

Dim pdDoc As Acrobat.CAcroPDDoc

Dim Jso As Object

Dim Annots() As Variant

Dim Annot As Variant

Dim Props As Object

Dim strContent, strSubType, strAuthor, strSubject As String

'Set AcrApp = New Acrobat.AcroApp
'Set AcrApp = CreateObject("AcroExch.App")
'Set AcrAvDoc = AcrApp.GetActiveDo

Set pdDoc = CreateObject("AcroExch.PDDoc")

pdDoc.Open ("C:\Users\OneCrazyStone\Desktop\H12_hdevelop_users_guide_JOM.pdf")

'Set pdDoc = AcrAvDoc.GetPDDoc

Set Jso = pdDoc.GetJSObject

Jso.syncAnnotScan  '//mr?? 这句还不能少

Annots = Jso.getAnnots()

Dim i As Integer
i = 0


For Each Annot In Annots

    Set Props = Annot.getprops

    strAuthor = Props.Author

    strSubject = Props.Subject

    strContent = Props.contents

    strSubType = Props.Type
    
    Debug.Print str(i) + " " + strAuthor + " " + strSubType + " " + strContent
    
Next

Set Props = Nothing

Set Annot = Nothing

'Set Annots = Nothing
Erase Annots

Set Jso = Nothing

Set pdDoc = Nothing

Set AcrAvDoc = Nothing

下面是VB.Net中的代码

Public Sub Main()
        'Dim SOURCE_DOCUMENT As String = System.Windows.Forms.Application.StartupPath + "\..\..\..\..\TestFiles\FormSample.pdf"
        'C:\Users\OneCrazyStone\Desktop\H12_hdevelop_users_guide_JOM.pdf
        Dim SOURCE_DOCUMENT As String = "C:\Users\OneCrazyStone\Desktop\H12_hdevelop_users_guide_JOM.pdf"

        Dim DATA_FILE As String = System.Windows.Forms.Application.StartupPath + "\..\..\..\..\TestFiles\data.txt"

        ' create at root level of sample
        Dim OUTPUT_FOLDER As String = System.Windows.Forms.Application.StartupPath + "/../../outputfolder/"
        System.IO.Directory.CreateDirectory(OUTPUT_FOLDER)
        Dim outputString As String
        Dim i As Short

        ' We gain access to the JavaScript DOM through the GetJSObject method
        ' of the CAcroPDDoc object.

        On Error GoTo ErrorHandler

        Dim retVal As Integer
        Dim count As Integer
        Dim srcDoc As Acrobat.CAcroPDDoc
        Dim srcApp As Acrobat.CAcroApp

        ' Open the source PDF document
        'srcApp = CreateObject("AcroExch.App")
        srcDoc = CreateObject("AcroExch.PDDoc")
        retVal = srcDoc.Open(SOURCE_DOCUMENT)


        If retVal <> -1 Then
            Err.Description = "Failed to open source document. Make sure that SOURCE_DOCUMENT has been configured."
            GoTo ErrorHandler
        End If

        ' Acquire the JSObject interface

        jsObj = srcDoc.GetJSObject
        jsObj.syncAnnotScan()


        Dim newDoc As Object
        Dim template As Object
        Dim existAnnotations() As Object
        Dim annots As Object
        Dim props As Object

        Dim aa As Object

        existAnnotations = jsObj.getannots()

        For Each aa In existAnnotations
            props = aa.getProps
            Debug.Print(props.Author + props.Contents)
        Next
      
EndSub:
        System.Runtime.InteropServices.Marshal.ReleaseComObject(jsObj)
        jsObj = Nothing
        If Not srcDoc Is Nothing Then
            srcDoc.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(srcDoc)
            srcDoc = Nothing
        End If
        Exit Sub

ErrorHandler:

        MsgBox("Error Occured: " & Err.Description)
        Resume EndSub

End Sub

在C#中调用Acrobat类型库, 如果也使用GetJSObject的话, 好像比较麻烦. 没有时间进一步研究

posted on 2020-10-23 01:50  OneCrazyStone  阅读(857)  评论(0编辑  收藏  举报

导航