Search code examples
javascriptmacrosadobeacrobat

Adobe Acrobat Action Wizard JavaScript. Create TextFiled and update TextField With current Date?


I am using the action wizard to perform some highlighting on bulk PDF documents. I would also like to add a DATE STAMP to the PDF file for the day it was processed (date Action Wizard Runs), and I think I am very close.

I can create the text field needed, but I am having problems updated to the text field the current date via the JavaScript code I have in action wizard. Why does this create the field, but does not add the date to it?

var fld = this.getField("MacroDate");
if ( fld==null ) {
    var f = this.addField("info", "text", 0, [11,11, 61,26]);
    f.delay = true;
    f.alignment = "center";
    f.fillColor = color.white;
    f.lineWidth = 1;
    f.strokeColor = color.black;
    f.borderStyle = style.s;
    f.textSize = 14;
    f.textColor = color.black;
    f.textFont = font.Arial;
    f.defaultValue = "MacroDate";
    f.editable = false;
    f.multiline = false;
    f.doNotScroll = true;
    f.delay = false;
    var TodayDate = this.getField("MacroDate");
    TodayDate.value = util.printd("mm/dd/yyyy", new date());
}

After doing some more digging I did manage to get it to work:

I am not exactly sure why it worked, but here is the code if anyone wants to explain it:

var AcDate = new Date();
var AcDateFormat = "mm/dd/yyyy"

for (var p = 0; p < this.numPages; p++){

    var fd = this.addField("xftDate", "text", 0, [10, 10, 100, 25]);
    fd.value =  util.printd(AcDateFormat, AcDate);
    fd.textSize = 16;
    fd.readonly = true;
    fd.alignment = "right";
}
flattenPages();

Solution

  • Just by a quick glance, the issue is in the last line:

    TodayDate.value = util.printd("mm/dd/yyyy", new date());
    

    In JavaScript, Capitalization does matter. And the Date Object is capitalized. Therefore:

    TodayDate.value = util.printd("mm/dd/yyyy", new Date()) ;
    

    You also should activate the Console, where you will get messages if errors occur.