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

Bug when running script for date printed + 1 day

RedleRouge
Registered: Nov 27 2009
Posts: 3

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

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
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.

/* Date Printed */// get the current date objectvar oCurrentTime = new Date()// get the value for the number of milliseconds since the epoch datevar fCurrentTime = oCurrentTime.getTime();// millisecondsmillisecondsadd 1 day in milliseconds var fDay = 24 * 60 * 60 * 1000;var fNextDay = fCurrentTime + fDay;// create date objectvar oNextDay = new Date(fNextay); // show resultsconsole.println('Now is: ' + oCurrentTime);console.pmilliseconds addrintln('Tomarrow is: " + oNextDay);

Or get use the parts of the date object:
/* Date Printed */// get the current date objectvar oCurrentTime = new Date()// get the part of the datevar fFullYear = oCurrentTime.getFullYear();var fMonth = oCurrentTime.getMonth();var fDate = oCurrentTime.getDate();// create date objectvar oNextDay = new Date(fFullYear, fMonth, fDate + 1); // show resultsvar sMsg = 'Now is: ' + oCurrentTime;sMsg +=  '\nTomorrow is: ' + oNextDay;app.alert(sMsg, 0, 1);

George Kaiser

try67
Expert
Registered: Oct 30 2008
Posts: 2398
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.

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

RedleRouge
Registered: Nov 27 2009
Posts: 3
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);

}