Solved

Acrobat Scripting - Expire a PDF

Posted on 2006-06-20
11
3,138 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:MHayes612
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 16945375
What is the problem with your script? What do you want it to do, and what does it do?
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 16945473
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
 

Author Comment

by:MHayes612
ID: 16945509
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
Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

 

Author Comment

by:MHayes612
ID: 16945545
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
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 16945546
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
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 16945563
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
 

Author Comment

by:MHayes612
ID: 16945583
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
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 16945635
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
 

Author Comment

by:MHayes612
ID: 16945707
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
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 500 total points
ID: 16945875
I see you are new to this site. Do you know how to close a question?
0
 

Author Comment

by:MHayes612
ID: 16945944
That was great Karl.

Thank you!
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Getting information about Fonts being used in a PDF file A colleague of mine recently faced an issue related to the PDF file format. The PDFs were containing mission critical client information, they were successfully mailed but there was a sm…
The Adobe PDF proprietary file format is recognized as secure and formulated. But these PDF files are also prone to corruption and any external threat like virus attacks, improper storage can hit PDF file integrity.This type of damages can make cruc…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question