Acrobat Scripting - Expire a PDF

Would anyone be willing to share or know of a script that could expire a PDF document after a certain amount of time.  I do not have the resources to purchase a DRM and am aware that users can change their own clock settings for a work around, but this functionality would be more then sufficient.

Ive gotten this far already but can't seem to get this to work.  I am new to JavaScript and hope someone can tell me where i am going wrong.

function Expire2()
{// Get the current date and time
var rightNow = new Date();

// Setup End Date
var endDate = new Date(2006, 5, 19);

// Calculate the time difference in milliseconds
var diff = rightNow.getTime() - endDate();

// Calculate the number of milliseconds in a day
//1 day * 24hrs/day x 60 min/hr x 60 sec/min x 1000 ms/sec
var oneDay = 24 * 60 * 60 * 1000;

// Calculate the number of days from current to end
var numOfDays = diff/oneDay;

if(numOfDays <= 0)
{
    app.alert("This Document has Expired.");
    this.close();
}
}


Any help is greatly appreciated.

Thanks - Mark
MHayes612Asked:
Who is Participating?
 
Karl Heinz KremerConnect With a Mentor Commented:
I see you are new to this site. Do you know how to close a question?
0
 
Karl Heinz KremerCommented:
What is the problem with your script? What do you want it to do, and what does it do?
0
 
Karl Heinz KremerCommented:
After looking at your script for a bit, I think I found out what's wrong (and what you want to do with it). Try this instead:

function Expire2()
{
    // Get the current date and time
    var rightNow = new Date();

    // Setup End Date
    var endDate = new Date(2006, 7, 19);

    if(rightNow.getTime() > endDate)
    {
        app.alert("This Document has Expired.");
        this.closeDoc();
    }
}


As you can see, it's much shorter. There is no need to calculate the actuall difference. All you need to do is compare the current data against the date the document expires. Also, you did call endDate like a function - this is wrong, it's a variable, so the "()" is not needed. The last problem was that there is no function called close(), it's closeDoc().

You need the AcroJS guide from adobe's web site. Do you have access to that document?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
MHayes612Author Commented:
I am not quite sure.  It doesn't seem to do anything.

Would like to be able to set the endDate prior to loading it into a pdf, so I will adjust that section on every PDF that i would have expire.

I am trying to OnOpen - run the script to determine the right now time.  Then, calculate the difference between now and my predetermined date.  If the result is less then or equal to 0, it should be expired and the alert should show up so it's not a surprise when it closes.

Any thoughts on that?

Thanks a lot for your quick response and any help you may be able to provide.

0
 
MHayes612Author Commented:
Man that was quick!

You solved it before i even got to reply to your first.  I started with JS yesterday and i did skim through the AcroJS guide and it was quite helpful, but as I am sure you can see, I could have spent some more time on it.

Let me give that a shot and I'll let you know how it worked.

Thanks a lot!
0
 
Karl Heinz KremerCommented:
To see what's wrong with your script, you can open the JavaScript debugger/console (Ctrl-J). It will show you one error at a time when you execute the script.

How do you call the script? Where do you store the script. You can create a document level JavaScript titled Expire2, and then call the Expire2() function right after you define it (as part of the same document level script).
0
 
Karl Heinz KremerCommented:
You should get a "normal" JavaScript book. As far as I know, there is nothing available that just discusses Acrobat JavaScript. This means that you have to work with a book that describes the browser level JavaScript. Just ignore anything besides the core language (which is the same for browsers and Acrobat).
0
 
MHayes612Author Commented:
I have it as a doc level script, and i am hopingthat it runs everytime you open the file.

Not sure how to call the script though.  I agree with the book idea.  Just so happens that this was brought up yesterday and was doning my best to make it happen.

Thanks Again!
0
 
Karl Heinz KremerCommented:
No, the document level JavaScript (if you use the function block that Acrobat gives you) is a function, and it will only run when you call it. The document level scripts get initialized when you start Acrobat. Edit the document level script and go all the way to the end - past the closing "}" and add the following line:

Expire2();

Save it, and open the document again (you may want to open your JavaScript console to see any potential errors). If you still don't see anything, add the following before the first line of your document level script (before the function... line):

app.alert("running document level script");

Save and try again. Do you see the popup?
0
 
MHayes612Author Commented:
That was it.  I was just about to ask that.

I included the expire2(); at the ensd of the script and it worked perfectly.

Thanks again, i really appreciate your help!
0
 
MHayes612Author Commented:
That was great Karl.

Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.