iTextSharp +检索表单按钮操作

时间:2011-07-27 15:44:55

标签: .net pdf itextsharp acrobat

iTextSharp是否有办法检索pdf中包含的按钮字段中包含的操作?

我想知道按钮字段在单击时(在MouseUp上)将执行的操作。

提前致谢

1 个答案:

答案 0 :(得分:1)

按钮信息在PDF中存储为注释。我wrote some code recently here that enumerated hyperlinks within a PDF(也是注释),我会在这里为你重新定位。

按钮的动作可以是一系列不同的东西,从JavaScript到菜单项到播放电影等等。下面的代码处理JavaScript,命名操作和目标操作,我将其他代码留给您。命名操作是特定于应用程序的,我不知道Adobe是否列出了它们的全部内容。请参阅上面的帖子,了解如何解决目标操作(InDirect参考)。

    Dim WorkingFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    Dim WorkingFile As String = Path.Combine(WorkingFolder, "Services.pdf")

    ''//Setup some variables to be used later
    Dim R As PdfReader
    Dim PageCount As Integer
    Dim PageDictionary As PdfDictionary
    Dim Annots As PdfArray
    Dim ActionObject, NamedAction As PdfObject
    Dim DestinationAction As PdfArray
    Dim ActionOjbectID As PdfObject
    Dim ActionDictionary As PdfDictionary

    ''//Open our reader
    R = New PdfReader(WorkingFile)
    ''//Get the page cont
    PageCount = R.NumberOfPages

    ''//Loop through each page
    For I = 1 To PageCount
        ''//Get the current page
        PageDictionary = R.GetPageN(I)

        ''//Get all of the annotations for the current page
        Annots = PageDictionary.GetAsArray(PdfName.ANNOTS)

        ''//Make sure we have something
        If (Annots Is Nothing) OrElse (Annots.Length = 0) Then Continue For

        ''//Loop through each annotation
        For Each A In Annots.ArrayList

            ''//Convert the itext-specific object as a generic PDF object
            Dim AnnotationDictionary = DirectCast(PdfReader.GetPdfObject(A), PdfDictionary)

            ''//Make sure this annotation is a button
            If Not AnnotationDictionary.Get(PdfName.FT).Equals(PdfName.BTN) Then Continue For

            ''//Make sure this annotation has an ACTION
            If AnnotationDictionary.Get(PdfName.A) Is Nothing Then Continue For

            ActionObject = AnnotationDictionary.Get(PdfName.A)

            If ActionObject.IsIndirect Then
                ActionOjbectID = PdfReader.GetPdfObject(AnnotationDictionary.Get(PdfName.A))
                If ActionOjbectID.IsDictionary Then
                    ActionDictionary = DirectCast(ActionOjbectID, PdfDictionary)
                    If ActionDictionary.Get(PdfName.JS) IsNot Nothing Then
                        Trace.WriteLine("JavaScript Action  : " & ActionDictionary.GetAsString(PdfName.JS).ToUnicodeString())
                    ElseIf ActionDictionary.Get(PdfName.N) IsNot Nothing Then
                        NamedAction = ActionDictionary.Get(PdfName.N)
                        Trace.WriteLine("Named Action       : " & NamedAction.ToString())
                    ElseIf ActionDictionary.Get(PdfName.D) IsNot Nothing Then
                        DestinationAction = ActionDictionary.GetAsArray(PdfName.D)
                        Trace.WriteLine("Destination Action : " & DestinationAction.ToString())
                    Else
                        ''//Add a bunch more 
                        Trace.WriteLine("Some other action  : ")
                        For Each K In ActionDictionary.Keys
                            Trace.WriteLine("                   : " & K.ToString())
                        Next
                    End If
                Else
                    ''//Not a dictionary, do something else here, should never reach this
                End If
            Else
                ''//Non InDirect reference, should never reach this
            End If
        Next
    Next

我应该注意,这会拉动按钮的“默认操作”,但可以在按钮上为各种状态执行多个操作。要获得这些内容,而不是查看PdfName.A,您需要查看PdfName.AA,这将为您提供一个您需要解决的InDirectReference