4 Replies Latest reply on Jul 17, 2014 8:24 AM by OzSanta2

    How can I add javascript text fields to Acrobat pages from Applescript please?

    OzSanta2

      I have a real need to add text fields to Acrobat document pages from Applescript. I can't access the Headers and Footers from Applescript in Acrobat XI. I can set watermarks using the following script, but cannot for the life of me work out how to add text fields. Any advice please?

       

      Regards

       

      set TempAttachmentName to "(20140717121212Z)"

      set PreserveFileName to "This is a test"

      set bypass to false

       

      tell application "Adobe Acrobat Pro"

        activate

        set CountOfPages to count of pages

        repeat with PageCounter from 1 to CountOfPages

        tell application "System Events" to tell process "AdobeAcrobat"

        try

        click menu item "Page..." of menu 1 of menu item "Go To" of menu 1 of menu bar item "View" of menu bar 1

        tell current application to delay 0.1

        keystroke PageCounter as text

        tell current application to delay 0.1

        keystroke return

        if not bypass then

        my ADDHeaderFooter(TempAttachmentName, PreserveFileName & " " & CountOfPages, PageCounter)

        end if

        end try

        end tell

        end repeat

      end tell

       

      on ADDHeaderFooter(The_String1, The_String2, currentPage)

        tell application "Adobe Acrobat Pro"

        activate

        set myDocument to document 1

        tell myDocument

        tell page currentPage

        set cbox to crop box

        set item 2 of cbox to (item 2 of cbox) + 44

        set item 4 of cbox to (item 4 of cbox) - 44

        set media box to cbox

        set crop box to cbox

        set theResult1 to my Add_WaterMarkText(The_String1, "IDAutomationHC39M", 12, 1, 1, 1, 0, 3, 10, 3, 0)

        do script theResult1

        set theResult2 to my Add_WaterMarkText(The_String2, "Times", 16, 1, 1, 1, 0, 4, 10, -6, 0)

        do script theResult2

        end tell

        end tell

        end tell

      end ADDHeaderFooter

       

      on Add_WaterMarkText(cText, cFont, nFontSize, nS, nE, nTA, nHA, nVA, nHV, nVV, nR)

        set Params to "cText: '" & cText & "'," & return

        set Params to Params & "cFont: '" & cFont & "'," & return

        set Params to Params & "nFontSize: '" & nFontSize & "'," & return

        if class of nS is integer then set Params to Params & "nStart: " & (nS - 1) & "," & return

        if class of nE is integer then set Params to Params & "nEnd: " & (nE - 1) & "," & return

        -- 0 = left, 1 = centre, 2 = right

        if class of nHA is integer then set Params to Params & "nHorizAlign: " & nHA & "," & return

        -- 3 = top, 4 = bottom

        if class of nVA is integer then set Params to Params & "nVertAlign: " & nVA & "," & return

        -- offset in points

        if class of nHV is integer then set Params to Params & "nHorizValue: " & nHV & "," & return

        -- offset in points

        if class of nVV is integer then set Params to Params & "nVertValue: " & nVV & "," & return

        if class of nR is integer then set Params to Params & "nRotation: " & nR & "," & return

        set javascript to "this.addWatermarkFromText({" & Params & "});"

        return javascript

      end Add_WaterMarkText

        • 1. Re: How can I add javascript text fields to Acrobat pages from Applescript please?
          Test Screen Name Most Valuable Participant

          Do you have the Acrobat JavaScript API? The second method for Doc will add a field.

          • 2. Re: How can I add javascript text fields to Acrobat pages from Applescript please?
            OzSanta2 Level 1

            G'day

             

            Unfortunately I don't have the API. My Adobe Reader installation updating to version XI failed, and subsequent attempts to fall back on the X version are blocked by the partial install of XI. Damn Adobe stuff.

             

            Could someone please post the relevant part of the API to give me some pointers, please.

             

            I ha mucked about with some information I found on the Web, and come up with the following, which fails because I can't work out how to address the current document with the variable myDoc.


            Regards, Brian


            set Params to Params & "var myDoc = this;" & return



            set TempAttachmentName to "(20140717121212Z)"

            set PreserveFileName to "This is a test"

            set bypass to false

             

            tell application "Adobe Acrobat Pro"

              activate

              set CountOfPages to count of pages

              repeat with PageCounter from 1 to 1 # CountOfPages

              tell application "System Events" to tell process "AdobeAcrobat"

              try

              click menu item "Page..." of menu 1 of menu item "Go To" of menu 1 of menu bar item "View" of menu bar 1

              tell current application to delay 0.1

              keystroke PageCounter as text

              tell current application to delay 0.1

              keystroke return

              if not bypass then

              my ADDHeaderFooter(TempAttachmentName, PreserveFileName & " " & CountOfPages, PageCounter)

              end if

              end try

              end tell

              end repeat

            end tell

             

            on ADDHeaderFooter(The_String1, The_String2, currentPage)

              tell application "Adobe Acrobat Pro"

              activate

              set myDoc to document 1

              tell myDoc

              tell page currentPage

              set cbox to crop box

              set item 2 of cbox to (item 2 of cbox) + 44

              set item 4 of cbox to (item 4 of cbox) - 44

              set media box to cbox

              set crop box to cbox

              set theResult1 to my Add_WaterMarkText(The_String1, "IDAutomationHC39M", 12, 1, 1, 1, 0, 3, 10, 3, 0, myDoc)

              do script theResult1

              set theResult2 to my Add_WaterMarkText(The_String2, "Times", 16, 1, 1, 1, 0, 4, 10, -6, 0, myDoc)

              do script theResult2

              end tell

              end tell

              end tell

            end ADDHeaderFooter

             

            on Add_WaterMarkText(cText, cFont, nFontSize, nS, nE, nTA, nHA, nVA, nHV, nVV, nR, myDoc2)

              #set Params to "var myDoc = app.newDoc();" & return

              set Params to ""

              set Params to Params & "var myDoc = this;" & return

              set Params to Params & "var inch = 72;" & return

              set Params to Params & "var Bbox = this.getPageBox(" & "\"Crop\"" & ");" & return

              set Params to Params & "var f = myDoc.addField(" & "\"Text1\"" & ", " & "\"" & cText & "\"" & ", 0, [72, Bbox[1]-inch, Bbox[2]-2*inch, Bbox[1]-2*inch ] );" & return

              set Params to Params & "f.strokecolor = color.black;" & return

              set Params to Params & "f.richText = true;" & return

              set javascript to Params

              return javascript

            end Add_WaterMarkText

            • 3. Re: How can I add javascript text fields to Acrobat pages from Applescript please?
              Test Screen Name Most Valuable Participant

              As a developer you need that document. It is part of the Acrobat SDK, which is free to download. You cannot learn Acrobat programming with google!

              • 4. Re: How can I add javascript text fields to Acrobat pages from Applescript please?
                OzSanta2 Level 1

                Thank you. I checked about faulty Adobe Reader installations, and had to remove several items from the main Library, and re-install version 10.x. I now have the API, and have used watermarks on each page to add barcodes and text.

                 

                Regards

                 

                Brian