9 Replies Latest reply on Aug 10, 2010 10:39 AM by BancLine

    Help with a JavaScript PDF Expiration date code

    Bushido19

      I need to set an expiration date to a PDF document, but the one I've got running so far keeps giving me the alert+close doc command even when the date is not right

       

      // --- start edit expiration date ---
        var lastDay=26
        var lastMonth=7
        var lastYear=2010
      // --- end edit expiration date ---
        var sessione
        var today=new Date();
        var myDate=new Date();
        lastMonth=lastMonth-1;
        myDate.setFullYear(lastYear,lastMont…‡
        if (myDate<=today && sessione!=1)
        {
           app.alert("Expiration date reached, sorry!!", 1, 0, "ALERT");
           sessione=1;
           this.closeDoc(true);
        }

       

      Basically what I want is that, reached X date (e.g: july 27. 2010) the document pops up an "Expiration date reached, sorry!!" alert and closes the document after OK is pressed

       

      And no, please don't tell me to get adobe rights manager because I'm on a Mac

        • 1. Re: Help with a JavaScript PDF Expiration date code
          try67 MVP & Adobe Community Professional

          This is a very bad way of securing the file. The user can overcome it by:

          1. Disabling JavaScript in Acrobat/Reader

          2. Changing the local time on their machine

           

          However, it's up to you to decide if you want to do it, I guess...

           

          The problem might be that you're trying to compare Date objects using

          mathematical operators (<=). You should first convert them to a number, like

          with the getTime method.

          Also, I don't see where you're applying any value to sessione before checking it.

          Also, there's something weird going on in this line, but I assume it's just

          a problem with the encoding:

          myDate.setFullYear(lastYear,lastMont…&#135;

          • 2. Re: Help with a JavaScript PDF Expiration date code
            Bushido19 Level 1

            Well, the ideal would be like the one in here: http://www.pdfscripting.com/public/100.cfm, but It just doesn't work. I'm really not familiar at all with JS, I'm a designer, not a programmer

             

            myDate.setFullYear(lastYear,lastMonth,lastDay); was the full line

            • 3. Re: Help with a JavaScript PDF Expiration date code
              try67 MVP & Adobe Community Professional

              If you'll notice, there it also says that the script can be beaten by anyone

              who knows a bit about JavaScript or Acrobat.

              For real protection you will need a DRM solution, which costs thousands of

              dollars.

              It's just a question of how well protected the file needs to be against how

              much you're willing to spend on it.

              • 4. Re: Help with a JavaScript PDF Expiration date code
                gkaiseril MVP & Adobe Community Professional

                First, Adobe occasionally changes the way that JavaScript works for some commands from version to version, and the ability to use 'this.close()' has been modified.

                 

                You line "myDate.setFullYear(lastYear,lastMonth,lastDay);" only sets the value of the year and ignores the other 2 parameters, because the 'setFullYear()' only has on parameter and uses that 1 value to set the full 4 digit year.

                 

                The sample script uses 'var finalDate = new Date("3/20/2009");' so why not just build the final date string value from you variables?

                 

                You can use something like 'var finalDate = new Date(lastMonth + "/" + lastDay + "/" + lastYear);'  to build the date string from your variables and the strings for the separators.

                 

                You have not obtained the value from the date object 'today'.

                 

                If you are going to change sample code, you should have the reference material for the language so you can read what the line of code is supposed t do. Then only change one line of code at a time and then observe what happens. In this way you would have to deal with the error from one change. And if you have an error, reread about the property, method, or function you are trying to use is supposed to do.

                • 5. Re: Help with a JavaScript PDF Expiration date code
                  BancLine Level 1

                  GKaiseril,

                   

                  function(){return A.apply(null,[this].concat($A(arguments)))}

                  First, Adobe occasionally changes the way that JavaScript works for some commands from version to version, and the ability to use 'this.close()' has been modified.

                   

                  Do you have any specifics what Adobe changed about the ability to use this.closeDoc()? Beginning in version 9.3.3 all our users get a fatal error that closes Reader / Acrobat. If I comment out that line the error goes away. Unfortunately, we need to close the doc to let our app. know it is time to process the next doc. So all our users had to roll back to version 9.3.2 and hope Adobe corrects this in the next release.

                   

                  Has anyone found another way to close the doc without throwing the fatal error?

                   

                  Thanks

                  • 6. Re: Help with a JavaScript PDF Expiration date code
                    gkaiseril MVP & Adobe Community Professional

                    From the Acrobat JS API Reference for the doc closeDoc method

                     

                    "It is important to use this method carefully, because it is an abrupt change in the document state that can affect any JavaScript executing after the close. Triggering this method from a Page event or Document event could cause the application to behave strangely.

                    "In versions of Acrobat earlier than 7.0, a document that closes itself by executing this.closeDoc terminates any script that follows it. In Acrobat 7.0, the script is allowed to continue and to terminate naturally. However, if the Doc of the closed document is referenced, an exception will be thrown."
                    JavaScript is also an interpretive language, so each line is read and then processed when a script is running. Closing the document in which this script is located causes a loss of the script so no more lines can be read and there is no place to report the completion of a line of code and pick up the next line of code or find the end of the script. Version 9 is just more strict about this action.
                    • 7. Re: Help with a JavaScript PDF Expiration date code
                      BancLine Level 1

                      GKaiseril,

                       

                      Yes,

                       

                      I'm aware of JS API Reference disclaimer on page 277. I am executing it from folder level script. The script hasn't changed in 2 years and worked fine in Adobe versions 7.0 through 9.3.2. It stopped working when Adobe released version 9.3.3.

                       

                      I can make the script do nothing but

                       

                      this.closeDoc(true);

                       

                      so that there is no reference to the document after it closes. I still get a fatal error:

                       

                      Expected a dict Object

                       

                      Followed by Microsoft Error report

                       

                      Followed by:

                       

                      Runtime error!

                      Program: C:\Program Files\Adobe\Reader9.0\Reader\Acrord32.exe

                       

                      R6025

                      -pure virtual function call

                       

                      Then Reader closes.

                       

                      All our users have rolled back to Reader 9.3.2 and the problem went away.

                       

                      I don't believe it is related to the location of the method ...rather something Adobe changed between version 9.3.2 and 9.3.3.

                       

                      Cheers!

                      Doug

                      • 8. Re: Help with a JavaScript PDF Expiration date code
                        gkaiseril MVP & Adobe Community Professional

                        Well it is obvious Adobe has changed the way this action works. It might be part of the long list of vulnerabilities fixed. The most probable detail would be access to a memory location outside of the memory space occupied by an open PDF and from that location being able to run rouge code.

                         

                        I do not work for Adobe, so if you want very specific information you will probably need to contact Adobe, but I really doubt they will give you the detailed reason.

                        • 9. Re: Help with a JavaScript PDF Expiration date code
                          BancLine Level 1

                          GKaiseril,

                           

                          I found something that may be related; If I execute this.closeDoc(true) from a button's mouse up event on Adobe 9.3.3, it closes correctly with no error.

                           

                          this.closeDoc(true); is currently being executed from a calculation script in a text field so it happens when the doc is initialized. I tried changing to a validation script and had the same fatal exception.

                           

                          Perhaps Adobe has restricted the availability of closeDoc and can no longer be called from calculation of validation scripts in version 9.3.3. If so, I am in need of another method to close the doc once it has printed with no user intervention.

                           

                          Thanks,

                          Doug