/ Id-Extras Forum / Document Action Close script?

  • Author
  • #6180 Reply

      I see how I can drop my JavaScript into FormMaker (this is very helpful), but I didn’t see a place where I can add a Document Action (Will Close) script. Is this possible?

      #6370 Reply

        Hi Mike,
        When you say, “I can drop my JavaScript into FormMaker”, you mean that it’s possible to add a general “document”-level script right?

        For now, it is true that there is no specific UI to add a Document Action. However, there’s a very simple workaround for this: When a document Javascript is added with FormMaker, it is run as soon as FormMaker is applied in Acrobat.
        So the answer is to add your document “Will Close” action by creating a document script that adds it.
        Once you’re ready to distribute the PDF, simply delete that particular document script from the PDF.
        To add a document action script is really simple. Here’s an example of adding a “WillSave” and “DidSave” action (from the Acrobat SDK):

        // WillSave script
        var myWillSave = 'var filesizeBeforeSave = this.filesize;'
        + 'console.println("File size before saving is " + '
        + ' filesizeBeforeSave );';
        // DidSave script
        var myDidSave = 'var filesizeAfterSave = this.filesize;'
        + 'console.println("File size after saving is "'
        + 'filesizeAfterSave);'
        + 'var difference = filesizeAfterSave - filesizeBeforeSave;'
        + 'console.println("The difference is " + difference );'
        + 'if ( difference < 0 )'
        + 'console.println("Reduced filesize!");'
        + 'else'
        + 'console.println("Increased filesize!");'
        // Set document actions...
        this.setAction("WillSave", myWillSave);
        this.setAction("DidSave", myDidSave);

        … and for for “WillClose” you’d do exactly the same.

        Does that make sense?

        #6915 Reply

          Hello Ariel,

          Is this to be added to the beginning of the script you want to add to “Will Save” and placed at the document level, or is it separate? If separate, how do you signify the particular script you want to put into “Will Save”?

          #6926 Reply

            Thank you for sharing this, Ariel! I worked through it and was able to make it function.

            #6927 Reply

              Thank you for sharing this, Ariel! I worked through it and was able to make my Will Close script write to that action.

              // reverts to user’s preferences
              var myWillClose = ‘app.runtimeHighlight = hiliUserPrefs;’
              +’ app.runtimeHighlightColor = colorUserPrefs;’;

              // Set document actions…
              this.setAction(“WillClose”, myWillClose);


              Just for aesthetics, is there a way to put a line break in there so that when it is written to Will Close, it will break it line by line?

              #6929 Reply

                Glad it you managed to get it work!

                For line breaks, try adding \r wherever a line break is needed in the script string (usually after each semicolon, etc.)

                If that doesn’t work, maybe \r\n or \n\r (line break + new line)

              Viewing 6 posts - 1 through 6 (of 6 total)


              Reply To: Document Action Close script?
              Your information: