开发者

iTextSharp + Retrieve Form Button Action

Is there any way with iTextSharp to retrieve the action contained in a button field contained in a pdf?

开发者_高级运维

I want to know what action that button field will do when clicked on (on MouseUp).

Thanks in advance


Button information is stored as Annotations within the PDF. I wrote some code recently here that enumerated hyperlinks within a PDF (also annotations) and I'll re-purpose it here for you.

The action of a button can be a bunch of different things, from JavaScript to menu items to playing a movie and more. The code below handles JavaScript, Named Actions and Destination Actions, I'll leave the others up to you. Named Actions are application-specific and I don't know if Adobe has a list of what they all are. See my post above for how to resolved a Destination Action (InDirect Reference).

    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

I should note that this pulls the "default action" for a button but its possible to have multiple actions on a button for the various states. To get those, instead of looking at PdfName.A you need to look at PdfName.AA which will give you an InDirectReference that you'll need to resolve.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜