6 Replies Latest reply on Jul 18, 2011 1:01 PM by George_Johnson

    Mapping Javascript Text to Buttons Automatically

    Joe The Engineer Level 1

      I know how to throw javascript into individual buttons in Acrobat.  However, we are generating a document with thousands of buttons, most of which will have similar javascript and will behave similarly (e.g. clicking on a button will open to a different page view, perhaps in another document in the same folder.)  I would like to be able to write the javascript text for each button using a VB macro to create a txt file for each button.  I know how to do this also. 

      What I do not know how to do, or even know if it is possible is to automate a process of tying the respective javascript that was generated in the txt files to buttons.  I assume the easiest way to do it is use a naming convention for each button that mimics the naming convention for my txt files.  This will allow for each button to identify with the text within it's respective txt file.  That leaves only one problem:  Finding a way to automate the transfer from each txt file to each button's javascript.  Can I write a script to do this?  If so, what is the best platform?  Can I do that with Javascript inside Acrobat?  I'm guessing that I can, but I'm so new to javascript that I'm not sure how to go about doing it.  fyi, i've written alot of vb code, which isn't the same, but at least i have a decent amount of code experience...i just need to learn javascript syntax and pdf structure. One last question:  Might it be possible and maybe even easier to insert the javascript from an external source like a VB program using the Adobe SDK?  Can the SDK do that?  I've messed with the SDK before and it appeared to have very limited operability on PDF's.

        • 1. Re: Mapping Javascript Text to Buttons Automatically
          George_Johnson MVP & Adobe Community Professional

          There are a number of ways you can do this. If it were me, I would create a single FDF file instead of multiple text files, and then import the FDF into the PDF, which would automatically set up the button actions for what you want. If you post a sample script, I could show a sample FDF the could do this.


          If you don't want to do that, you can use IAC to set all of the button actions. Using the JavaScript object (JSO) might be easiest, using the field.setAction method. More information is in the Acrobat SDK. For example: http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/IAC_DevApp_OLE_Support.100.1 3.html


          So you could create a VBScript that opens the document, loops through the text files, reads them, and sets the button actions.

          • 2. Re: Mapping Javascript Text to Buttons Automatically
            Joe The Engineer Level 1



            I've spent some time messing around with all of the options discussed above.  This leaves me with some questions:


            1)  Is it possible to use javascript to set the "run a javascript" text into a button?  Right now, I'm manually inserting the javascript into each button. The only way that I can see all of the code in once place is goto Advanded, Document Processing, EditAll Javascripts.  I'm thinking it might be possible with a batch process.


            2)  The JSObject being used in VB is useful in doing some things, but I haven't found a way to use it to insert code into a button field.  I can identify each button field and do some things with each button...but I don't know of a way to insert the javascript that I'd like to execute into the button.  Does anyone know if that's possible?


            3) You mention an FDF file.  I was trying to figure out exactly how to use it.  It looks like a format that stores user input into fields without all the other junk that comes into a PDF.  I created an FDF file from my PDF (which has only buttons, no text fields) and then viewed it as a .txt file to see what it would look like.  There wasn't much there.  For a PDF of 250 pages, the FDF file was something like 4 lines of text.  So either I'm not accessing the FDF file correctly, or the FDF file doesn't contain information concerning buttons.


            4)  VB can access some things that I'm not entirely sure about...  There is a "perform" method associated with annotations, but I'm not entirely sure how to use it.  I noticed I can use the jsObject to do some things, but not actually edit or create javascript within a button. 

            • 3. Re: Mapping Javascript Text to Buttons Automatically
              George_Johnson MVP & Adobe Community Professional

              1. Yes, using the field.setAction method: http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.750.html


              2. See #1


              3. It is a specially constructed FDF, different from one that contains only field name/value pairs. I usually use the FDF Toolkit to create such FDF, but it can be done by other means as well.


              4. See #1

              • 4. Re: Mapping Javascript Text to Buttons Automatically
                maxwyss Level 4

                In addition to George's answer to #3, you should have a closer look at the FDF section in the PDF specification (ISO 32000), part of the Acrobat SDK documentation (if I remember correctly, it is section 12.7.7).


                FDF can much more than just transfer data from and to a PDF document, as it has been said.


                And, it is rather easy to use a database to create the "core" of the FDF (and then either automatically or manually add the "additional stuff", making it a valid FDF. In fact, yours truly has been doing such things for more than a decade now…


                Hope this can help.


                Max Wyss.

                • 5. Re: Mapping Javascript Text to Buttons Automatically
                  Joe The Engineer Level 1

                  Thanks to everyone who has helped me learn this stuff.  I've been looking at the pages and references that you have given me so far.  Now I can manipulate PDF with VBA code written in excel.  It's very similar to the scrips shown for a standalone VB program, just some differences in document declaraion (particularly in using objects).   I've noticed in the VBA (excel) script i can't get the "props" of an existing annotation, but I can for one that I'm just creating....




                  'Defining Stuff, I have alot more variables but I'm just showing the ones pertinent to my quandry.


                  Dim acroannotation As Acrobat.AcroPDAnnot

                  Dim props As Variant
                  Dim myAnnot As Object




                  'This works:



                  Set myAnnot = jso.AddAnnot

                  Set props = myAnnot.getProps



                  'This doesn't:


                  Set acroannotation = Acropage.GetAnnot(1)

                  Set myAnnot = acroannotation

                  Set props = myAnnot.getProps



                  I noticed in the SDK, the examples show props being defined as an object just like 'myAnnot.'  However, at the end of the VB tutorial, it indicates that I should use variant for most JSObject references.  That doesn't really matter.  What does matter is that I can't figure out how to get the 'props' object from an existing annotation.  Am I doing something wrong in trying to reference it?  It gives me the ole' Runtime Error 438: Object does not support this property or method, which usually means a type mismatch.  I also tried "Set props = acroannotation.getProps", thinking that actually referencing the Annotation object instead of the "Object" object, but I still got the same error.


                  Finally, when I do figure out how to getProps for an existing annotation (the annotation is a button by the way, but I don't think that matters), I still have yet to find any reference as to how to use the "setAction" method for a button through the JSObject.


                  Thanks for any input on the matter.

                  • 6. Re: Mapping Javascript Text to Buttons Automatically
                    George_Johnson MVP & Adobe Community Professional

                    When working with forms fields, you should be using the forms API: http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/IAC_API_FormsIntro.107.2.htm l


                    Although you can do things indirectly via the JSObject too. A button is a type of form field.