Javascript Expire Code

I have been using a Expire Code someone provided to me from this site. Works great. The thing I've noticed is that when I apply the code to a multi-page doc the code doesn't close the doc. Instead an alert window pops open stating the expiration date has passed. There's a "OK" option to click. Then another window pop open asking "Don't save, Cancel, or Save". If I click Don't Save, the doc closes. If I click Save a window pops open asking where to save the doc to. But, if I click Cancel the doc stays open and the Expire Code is defeated. The user then has complete access. If they save their work they'll still get the Expire window, but nonetheless, they'll be able to continue using my work.

When I apply the Expire Code to a single page doc the code shuts down right on time the way I want. But not when it comes to a multi-page doc. Can someone shed some light as to what is going on with the code and possibly help me correct it so it will work on a multi-page doc. Here's the script I've been using, please take a look and let me know. Thanks.

function Expire()
{
// Get the current date and time
var rightNow = new Date();
// Setup End Date
var rightNow = new Date();
var beginDate = new Date("December 5, 2012 06:15:00 AM");
var endDate = new Date("June 16, 2013 1:57:00 PM");
if (rightNow < beginDate || rightNow > endDate)
{
app.alert("It has past the time limit. You cannot use this document any further.\n___________________________________________\n\nContact me to continue using this document. Thanks.", 0, 0);
this.closeDoc()
}
}

// execute check expiration code
Expire();


jackson chaker


5 Answers

Voted Best Answer

I guess you don't want to let the user save or anything with the document; you simply want it to close.

This can be accomplished by changing the closeDoc line to

this.closeDoc(true) ;

And that should take care of it.

Hope this can help.

Max Wyss.


By Max Wyss   

Since all of this depends upon JavaScript and the user can turn off the use of JavaScript in Acrobat and Reader you may find it is of limited use.

See the closeDoc() method for additional restrictions.

If you are going to compare dates and times, you need to convert them to a numeric value or a character string that is sequential for date and time, like using the ISO date format of YYYYMMDD and 24 hour time.


George Kaiser   

This code doesn't provide a complete solution. As George stated, it doesn't protect the PDF when JavaScript is turned off or when the PDF is viewed in a non-Adobe PDF viewer. You're also running up against timing issues with document scripts, i.e. in some larger documents, this script will run before the PDF is fully loaded. I imagine Acrobat is getting confused and thinks the PDF is dirty.

One solution is to force the close without saving:
this.closeDoc(true);

A much better solution can be found here (this is a membership site):
Sample PDF Downloads

Look for the sample labeled "Lite Doc Security Technique".

Thom Parker
The source for PDF Scripting Info pdfscripting.com
All About PDF Stamps in Acrobat and Paperless Workflows - THE BOOK !!

The Acrobat JavaScript Reference, Use it Early and Often

The most important JavaScript Development tool in Acrobat
The Console Window (Video tutorial)
The Console Window(article)

Having trouble, Why Doesn't my Script Work


Thom Parker   

Hi guys, I used this code in a PDF document and it worked no problem on an imac, but when trying to use it with PDF Expert on a tablet, the error message appears but the document still stays open. Is there a way to have this work for apps such as PDF Expert?

Thanks and LOOOVE the advice you guys dish out, really helpful and well written.

Ian.


Ian Allen   

Mobile devices do not support all the possible Acrobat JS features. It ranges form none to sum.

Many mobile apps do not support the "function" and a lot of the various document actions like format.

That is why many responders advise others to test the PDF on the apps the users will be using.


George Kaiser   


Please specify a reason: