Step 1: Check us out

You don't have to be a member to look at any content on the site. Increase your expertise with our helpful tutorials, videos, forums, and sample PDFs.

Step 2: Sign up for a free account

Like what you see? Take the next step and become a member. Register now to get discounts, attend eSeminars, ask questions and more.

Step 3: Start participating

Get the most out of your membership. Post in the forums, create your profile, submit to the gallery, attend a user group meeting. Log In now.

Acrobat User Community Forums

You are not logged in.     Log in to your AUC account.     Don't have an account? Sign up today

#1 2009-11-27 10:36:58

RedleRouge
Member
Registered: 2009-11-27
Posts: 2

Bug when running script for date printed + 1 day

Hello,

I use a script to print the print date + 1 day on a PDF document. This date appears only when the document is printed.

The script works but I get error messages that I cannot debug.

Here is the script:

/* Date Printed */
var currentTime = new Date()
currentTime.setDate(currentTime.getDate()+1);
for (var p = 0; p < this.numPages; p++)
{ var fd = this.addField("Date", "text", p, [100,10, 460,40]);

fd.textSize=12; fd.value = "UNCONTROLLED COPY - VALID UNTIL: " + util.printd("dd mmm yyyy", currentTime);

fd.allignment = "center"; fd.fillColor = color.black; fd.textColor = color.white; fd.display = display.noView;


var txtWillPrint = 'var d = this.dirty;\r'
+ 'var fd = this.getField("Date");\r'
+ 'fd.value = "UNCONTROLLED COPY - VALID UNTIL: " + '
+ 'util.printd("dd mmm yyyy", currentTime);\r'
+ 'this.dirty = d;';

this.setAction("WillPrint", txtWillPrint);

}


And the error messages:

InvalidGetError: Get not possible, invalid or unknown.
Doc.layout:7:Batch undefined:Exec

currentTime is not defined
3:Document-Actions:Document Will Print
currentTime is not defined
3:Document-Actions:Document Will Print

Thanks in advance.

Red


Offline

 

#2 2009-11-27 13:46:17

gkaiseril
Expert

From: Chicago, IL US
Registered: 2006-02-24
Posts: 3212

Re: Bug when running script for date printed + 1 day

First the JavaScript Date object is not a number and not a date string, so you need to covert it to a number or get the year, month and date from the date object and then adjust the necessary values for 1 day. If you use the 'getTime()' or 'valueOf()' methods to get the date time value from the date object, the value will be in millisecods from the epoch date and not days. You can then add the necessary time interval in milliseconds to get a new date time value and then number can then be converted back to a date object and formatted.

Code:

/* Date Printed */ 
// get the current date object
var oCurrentTime = new Date() 
// get the value for the number of milliseconds since the epoch date
var fCurrentTime = oCurrentTime.getTime();
// millisecondsmillisecondsadd 1 day in milliseconds 
var fDay = 24 * 60 * 60 * 1000;
var fNextDay = fCurrentTime + fDay;
// create date object
var oNextDay = new Date(fNextay);

// show results
console.println('Now is: ' + oCurrentTime);
console.pmilliseconds addrintln('Tomarrow is: " + oNextDay);

Or get use the parts of the date object:

Code:

/* Date Printed */ 
// get the current date object
var oCurrentTime = new Date() 
// get the part of the date
var fFullYear = oCurrentTime.getFullYear();
var fMonth = oCurrentTime.getMonth();
var fDate = oCurrentTime.getDate();
// create date object
var oNextDay = new Date(fFullYear, fMonth, fDate + 1);

// show results
var sMsg = 'Now is: ' + oCurrentTime;
sMsg +=  '\nTomorrow is: ' + oNextDay;
app.alert(sMsg, 0, 1);

Offline

 

#3 2009-11-27 19:16:15

try67
Expert

Registered: 2008-10-30
Posts: 1177

Re: Bug when running script for date printed + 1 day

I think the problem is that you define currentTime in your general script (which you didn't say where you run it), but not in your embedded script. So when the embedded script is run, it can't find any variable called currentTime. Hence the error message.


Check out my custom-made scripts website: http://try67.blogspot.com

Offline

 

#4 2009-11-30 08:12:39

RedleRouge
Member
Registered: 2009-11-27
Posts: 2

Re: Bug when running script for date printed + 1 day

Thanks for the helpful answers.

I revised the script and it is now running without bug. Here it is for info:

/* Date Printed */
// get the current date object
var oCurrentTime = new Date()
// get the part of the date
var fFullYear = oCurrentTime.getFullYear();
var fMonth = oCurrentTime.getMonth();
var fDate = oCurrentTime.getDate();
// create date object
var oNextDay = new Date(fFullYear, fMonth, fDate + 1);

/* Date Printed */

for (var p = 0; p < this.numPages; p++)
{ var fd = this.addField("Date", "text", p, [100,10, 460,40]);

fd.textSize=12; fd.value = "UNCONTROLLED COPY - VALID UNTIL:  " + util.printd("dd mmm yyyy", oNextDay);

fd.allignment = "center"; fd.fillColor = color.black; fd.textColor = color.white; fd.display = display.noView; 

//Print the date

var txtWillPrint = 'var d = this.dirty;\r' 
  + 'var fd = this.getField("Date");\r'

'var fFullYear = oCurrentTime.getFullYear();'

  + 'var oNextDay = new Date(fFullYear, fMonth, fDate + 1);'

  + 'fd.value = "UNCONTROLLED COPY - VALID UNTIL:  " + ' 

  + 'util.printd("dd mmm yyyy", oNextDay);\r' 

  + 'this.dirty = d;'; 
 

this.setAction("WillPrint", txtWillPrint); 
 
}

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson