These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Batch processing of javascript for page properties

Djay
Registered: Nov 5 2008
Posts: 10

Hi everyone,

Here's my problem, I think I have searched everywhere to get the solutions, but I didn't find it...

I have to protect a lot of PDF files with a date expiration protection.
Manually, I open a PDF, I open the "Pages" panel on the left, I right-click the first page, and I add a script on the "open page" trigger. Here's the script:

CheckExpiration()
 
function CheckExpiration()
{
/*-----START EDIT-----*/
var LastDay = 31
var LastMonth = 12
var LastYear = 2008
/*-----END EDIT-------*/
 
/* DO NOT EDIT PAST HERE !!! */
var today = new Date();
var myDate=new Date();
LastMonth = LastMonth - 1
myDate.setFullYear(LastYear,LastMonth,LastDay);
 
if (myDate<today) { this.closeDoc(1); app.alert("File has expired",1,0,"Expired"); }
 
}

I save my PDF, and it works.

(I know that if I change the date of my computer, I can open the PDF, but this is not a problem).

Now, the problem, is that I have to make this on thousands files.
Thus, I 've tried to make a batch process, using javascript.
But it appears that I can't treat javascripts for pages, just for the document.
I' ve tried with the code above, it doesn't work. It's normal.

After that, I 've tried another thing:
I opened the PDF file that I have protected manually.
Then, I've been in menu Advanced>Document processing>edit all javascripts
and I 've got this following code:

//-------------------------------------------------------------
//-----------------Do not edit the XML tags--------------------
//-------------------------------------------------------------
 
//<Page-Actions>
//<ACRO_source>Page1:Page Open:Action1</ACRO_source>
//<ACRO_script>
/*********** belongs to: Page-Actions:Page1:Page Open:Action1 ***********/
CheckExpiration()
 
function CheckExpiration()
{
/*-----START EDIT-----*/
var LastDay = 31
var LastMonth = 12
var LastYear = 2009
/*-----END EDIT-------*/
 
/* DO NOT EDIT PAST HERE !!! */
var today = new Date();
var myDate=new Date();
LastMonth = LastMonth - 1
myDate.setFullYear(LastYear,LastMonth,LastDay);
 
if (myDate<today) { this.closeDoc(1); app.alert("File has expired",1,0,"Expired"); }
 
}
//</ACRO_script>
//</Page-Actions>

I've tried to apply this code, by the batch processing... and it doesn't work.

I don't know what to try now, if I can put a javascript on the document, which makes the same protection, or if there's another solution.

Thank you if someone can help me.

Djay

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
In your batch process, how are you attempting to add the code to a page action? Are you using the doc.setPageAction method? You could also use the doc.addScript method or by importing an appropriately configured FDF.

George
Djay
Registered: Nov 5 2008
Posts: 10
Hi George,
Thank you for your quickness...

This is how I've tried to make the batch:

Advanced>Document processing>Batch processing
New sequence
Select command: Javascript>Execute javascript -> ADD
Then, I 've pasted the second code (see above)
Select folder and Run sequence.


But now that you told me about the doc.setPageAction method, I've read the JavaScript™ for Acrobat® API Reference to get informations about this method.

I've tried to batch the following code, but it does nothing.
And I constat that the file which supposed to be resaved, is not resaved after the batch...

this.setPageAction(0, "Open");{  CheckExpiration() function CheckExpiration(){/*-----START EDIT-----*/var LastDay = 31var LastMonth = 12var LastYear = 2009/*-----END EDIT-------*/ /* DO NOT EDIT PAST HERE !!! */var today = new Date();var myDate=new Date();LastMonth = LastMonth - 1myDate.setFullYear(LastYear,LastMonth,LastDay); if (myDate<today) { this.closeDoc(1); app.alert("File has expired",1,0,"Expired"); } }}

- I don't know if my code is correct
- I can't explain why my file isn't saved after the batch
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
No, that is not the correct usage of setPageAction. You need to pass the text of your script, properly formatted/escaped, as the third parameter. An example is shown in the Acrobat JavaScript documentation.

George
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Check your "setPageAction()" parameters. You have the page number and the trigger, but have not included the code to be executed parameter.

var nPageNum = 0;
var cPageAction = "Open";
var cScriptCode = "app.beep(0);"

this.setPageAction(nPageNum, cPageAction, cScriptCode);

One could also use the "docuemnt level' function for executing this code.

I would also define the function before the call to the function.

You do realize that anyone turning off JavaScript within Reader or Acrobat will stop this technique from working.

George Kaiser

Djay
Registered: Nov 5 2008
Posts: 10
Sorry for the delay, I was away few days.
Then, I've made some searches in the acrobat documentation, in order to not ask stupid questions...

@gkaiseril: I know it's a ridiculous kind of protection. But I have to do it like this...


Here are the 2 methods I've tested, by putting the following scripts in batch sequences:


First method (SetPageAction):



var nPageNum = 0;var cPageAction = "Open";var cScriptCode = "/*-----START EDIT-----*/var LastDay = 31

var LastMonth = 12

var LastYear = 2009

/*-----END EDIT-------*/

 /* DO NOT EDIT PAST HERE !!! */

var today = new Date();

var myDate=new Date();

LastMonth = LastMonth - 1

myDate.setFullYear(LastYear,LastMonth,LastDay);

 if (myDate<today) { this.closeDoc(1); app.alert("
File has expired",1,0,"Expired"); }"  this.setPageAction(nPageNum, cPageAction, cScriptCode);

Remarks: In the example, the script of the var cScriptCode is in one line, buit in this case my script has to be on several lines. I think it causes a problem.

Results: Doesn't work




Second method (AddScript):

I think it's a better solution for this problem, but the example in the reference PDF is also in one line...

this.addScript("My Code", "app.beep(0);");

I've tried to conform this code to my problem:

function CheckExpiration(){/*-----START EDIT-----*/var LastDay = 31var LastMonth = 12var LastYear = 2009/*-----END EDIT-------*/ /* DO NOT EDIT PAST HERE !!! */var today = new Date();var myDate=new Date();LastMonth = LastMonth - 1myDate.setFullYear(LastYear,LastMonth,LastDay); if (myDate<today) { this.closeDoc(1); app.alert("File has expired. Use updated files from Bobcat publications",1,0,"Expired"); } } this.addScript("My Code", "CheckExpiration();");

Remarks:
- I've tested batch of this script and when I consult the pdf after that, I see in the script that the name of the function is written, but not the rest.
- Even if it works, I'm not sure that the script will be launched when I open my document. Maybe do I have to put a event trigger like App.DocOpen for example.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You have to use either the "setPageAction()" or "setAction()" method in your batch process.

The batch process JavaScript using JavaScript could be:
// define parameters for setPageAction
var nPageNum = 0; // page 1
var cPageAction = "Open"; // action
var cScriptCode = "app.beep(0); // beep\napp.alert(\"Page Opened\"); // alert message\n" // script to run beep and alert
this.setPageAction(nPageNum, cPageAction, cScriptCode); // add the script to page 1

The JavaScirpt for creating a document level script by batch processing:

var cScriptName = "MyScript"; // name of script
var cScriptCode = "app.beep(0); // beep\napp.alert(\"Page Opened\"); // alert message\n" // script to run beep and alert
this.addScript(cScriptName, cScriptCode); // add the script as a document level script

Note for both methods you need to create a string variable that has the value of the entire script to be added not just one line of the script. The JavaScript Reference contains information on how to include special control characters like quotes and new lines within a string variable.

George Kaiser

Djay
Registered: Nov 5 2008
Posts: 10
OK,
Thank you to have put me on the right way !



I've chosen the document-level script method.

First, I put this script as a document javascript:

CheckExpiration() function CheckExpiration(){var LastDay = 31;var LastMonth = 12;var LastYear = 2009; var today = new Date();var myDate=new Date();LastMonth = LastMonth - 1;myDate.setFullYear(LastYear,LastMonth,LastDay); if (myDate<today){this.closeDoc(1);app.alert("File has expired",1,0,"Expired");} }

It works !
But after my alert, I have another error alert reporting me a bad parameter. I don't understand why.



After that, I put this script in a string variable, to use it with the AddScript function.
I've made this line by line, separated by the \n. Everything is OK, except the last line of my code.


I put this code, and it works...
var cScriptName = "CheckExpiration"; var cScriptCode = "CheckExpiration()\nfunction CheckExpiration()\n{\nvar LastDay = 31;\nvar LastMonth = 12;\nvar LastYear = 2009;\nvar today = new Date();\nvar myDate=new Date();\nLastMonth = LastMonth - 1;\nmyDate.setFullYear(LastYear,LastMonth,LastDay);\nif (myDate<today)\n{\nthis.closeDoc(1);\n}\n}"; this.addScript(cScriptName, cScriptCode);

... but this last command is missing
app.alert("File has expired.",1,0,"Expired");
When I try to add this last line in the code, it's refused because:
SyntaxError: missing ; before statement
3:(null) at line 4


var cScriptName = "CheckExpiration"; var cScriptCode = "CheckExpiration()\nfunction CheckExpiration()\n{\nvar LastDay = 31;\nvar LastMonth = 12;\nvar LastYear = 2009;\nvar today = new Date();\nvar myDate=new Date();\nLastMonth = LastMonth - 1;\nmyDate.setFullYear(LastYear,LastMonth,LastDay);\nif (myDate<today)\n{\nthis.closeDoc(1);\napp.alert("File has expired.",1,0,"Expired");\n}\n}"; this.addScript(cScriptName, cScriptCode);

I've tried to add the missing ";", but it changes nothing
I don't understand why this last command makes problem.
I've searched into the different reference docs, but they aren't not very explicit about this.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Replace the double quotation-marks (") with single ones (').

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Djay
Registered: Nov 5 2008
Posts: 10
Thank you try67 !
My batch script is now accepted !

It remains just the "Bad parameter" problem, I know it comes from my document-level script.

I will check all lines.
Djay
Registered: Nov 5 2008
Posts: 10
It's done, I had to invert my two last commands, and put the this.close in last position.

Thank you to all of us for this !

Here's my final batch script:

var cScriptName = "CheckExpiration"; var cScriptCode = "CheckExpiration()\nfunction CheckExpiration()\n{\nvar LastDay = 31;\nvar LastMonth = 12;\nvar LastYear = 2009;\nvar today = new Date();\nvar myDate=new Date();\nLastMonth = LastMonth - 1;\nmyDate.setFullYear(LastYear,LastMonth,LastDay);\nif (myDate<today)\n{\napp.alert('File has expired');\nthis.closeDoc(1);\n}\n}"; this.addScript(cScriptName, cScriptCode);