Skip to main content
Participating Frequently
November 1, 2005
Question

Add current date to PDF documents

  • November 1, 2005
  • 127 replies
  • 43042 views
This doesn't seem like it should be hard, but I have been unable to find information on how to do it.

I have about 100 PDF files. Somewhere on them, I need to display "Date Printed: dd mmm yyyy", and have the current date displayed.

I don't care whether this is placed at the top of the page, the bottom, or opaque in the background.

I would like to be able to accomplish it using the batch command, but will perform manually if necessary.

I have Acrobat 7 Professional.

Can someone please help me out?
    This topic has been closed for replies.

    127 replies

    Thom Parker
    Community Expert
    Community Expert
    January 19, 2006
    Dave,
    The variable named "theDate" is not a function, delete the trailing parenthesis, they are causing an exception.

    Whenever something doesn't work you should take a look at the console window, any errors will be reported there. In this case, the first thing you need to do is make sure that the script is being written to the document. If it is, then copy it to the console window and try it there. This will make any errors more obvious and easier to fix.

    Good Luck,
    Thom Parker
    WindJack Solutions
    www.windjack.com
    Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
    Participating Frequently
    January 19, 2006
    Thom -<br /><br />I guessed at how to implement the line of code you sent....but it does not work. The "Document Expires...." message does not display at all...<br /><br />Here is the entire script...please tell me what I need to do...<br /><br />// Check for existing field and make sure it's on the first page<br /> var strModName = "DocMessage";<br /> var ofield = this.getField("DocMessage");<br /><br /> // Create Field if it doesn't exist<br /><br /> if(ofield == null)<br /> {<br /> // Field is the width of the page, on the bottom of the page<br /> // and 40 points high (72pts/inch)<br /><br /> for(var i=0;i<this.numPages;i++)<br /> {<br /> var rectField = this.getPageBox("Crop",i);<br /> rectField[1] = rectField[3] + 14; // Set Top<br /> ofield = this.addField({cName: strModName,cFieldType: "text",<br /> nPageNum:i ,oCoords: rectField});<br /> ofield.alignment = "center"; //This is the text alignment of the field<br /> ofield.multiline = true;<br /> ofield.textSize = 8; <br /> ofield.textFont = "Arial";<br /> ofield.display = display.noView; //This is hidden on the page but printable to make viewable change to display.visible or noView<br /> ofield.readonly = true;<br /> }<br /> }<br /><br /> // Add Field to Document Script<br /> var strJS = 'var ofield = this.getField("DocMessage");';<br /> strJS += 'var theDate = (new Date((new Date()).valueOf() + 1000 * 60 * 60 * 24 * 14));';<br /> strJS += 'ofield.value = "Intellectual Property of Hematech - CONFIDENTIAL. ";';<br /> strJS += 'ofield.value += " Document expires " + util.printd("ddmmmyyyy",theDate()) + ".";';<br /> this.addScript("AddMessage",strJS);
    Participant
    January 19, 2006
    Ooops - nevermind - realized the comment fields needed to be broken up on new lines
    Participant
    January 19, 2006
    OK - I'll admit I'm very new to this, but can understand why it wont work...<br /><br />I have created the batch script as shown above and run it on my document.<br /><br />Nothing happens at all - nothing shows when printed! What am I doing wrong?<br /><br />This seems like something most people would like to do - why is there not an easy step by step on how to add this stamp on every page! Grrrrrr<br /><br />I included a snip of the code below...<br /><br />Scott<br /><br />// Check for existing field and make sure it's on the first page <br /> var strModName = "DocMessage"; <br /> var ofield = this.getField("DocMessage"); <br /><br />// Create Field if it doesn't exist <br /><br />if(ofield == null) <br /> { <br /> // Field is the width of the page, on the bottom of the page <br /> // and 40 points high (72pts/inch) <br /><br />for(var i=0;i<this.numPages;i++) { var rectField = this.getPageBox("Crop",i); rectField[1] = rectField[3] + 14; // Set Top ofield = this.addField({cName: strModName,cFieldType: "text", nPageNum:i ,oCoords: rectField}); ofield.alignment = "center"; //This is the text alignment of the field ofield.multiline = true; ofield.textSize = 8; ofield.textFont = "Arial"; ofield.display = display.noView; //This is hidden on the page but printable to make viewable change to display.visible or noView ofield.readonly = true; } } <br /><br />// Add Field to Document Script <br /> var strJS = 'var ofield = this.getField("DocMessage");'; <br /> strJS += 'ofield.value = "Intellectual Property of My Company - CONFIDENTIAL. ";'; <br /> strJS += 'ofield.value += " Document printed " + util.printd("ddmmmyyyy",new Date()) + ".";'; <br /> this.addScript("AddMessage",strJS);}}
    Participating Frequently
    January 19, 2006
    Thanks Thom - but where in my original script do I put this?
    Thom Parker
    Community Expert
    Community Expert
    January 19, 2006
    Hello Dave,
    This script creates a script that is then added to the current document. Lets call it the creation script. The script that is created places a value (a date) into a specific field on the document, so lets call it the Date script. The date is static because its setup in the creation script, i.e. it's set at the time the Date script is created. If you want the date to be set dynamically, then you have to place the code that sets it up into the Date Script. The following line calculates a date that is 14 days in the future.

    var theDate = (new Date((new Date()).valueOf() + 1000 * 60 * 60 * 24 * 14));

    Use it to modify the Date Script

    Thom Parker
    WindJack Solutions
    www.windjack.com
    Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
    Participating Frequently
    January 17, 2006
    bump.

    Anyone?
    Participating Frequently
    January 12, 2006
    Dimitri -<br /><br />I made the change you suggested, and the message prints....but it is a static date..not a dynamic date. The goal is to have the message display a future date 14 days from the date printed. Yesterday I made a test document, printed it and the message said "Document Expires 25JAN2006. Which was correct. I opened and printed the same document this morning and the messge still printd with 25JAN2006. It should say 26JAN2006 today.<br /><br />What am I doing wrong?<br /><br />Here is the exact script I've run as a batch...including your change.<br /><br />/* Put script title here */<br /><br /> // Check for existing field and make sure it's on the first page<br /> var strModName = "DocMessage";<br /> var ofield = this.getField("DocMessage");<br /><br /> // Create Field if it doesn't exist<br /><br /> if(ofield == null)<br /> {<br /> // Field is the width of the page, on the bottom of the page<br /> // and 40 points high (72pts/inch)<br /><br /> for(var i=0;i<this.numPages;i++)<br /> {<br /> var rectField = this.getPageBox("Crop",i);<br /> rectField[1] = rectField[3] + 14; // Set Top<br /> ofield = this.addField({cName: strModName,cFieldType: "text",<br /> nPageNum:i ,oCoords: rectField});<br /> ofield.alignment = "center"; //This is the text alignment of the field<br /> ofield.multiline = true;<br /> ofield.textSize = 8; <br /> ofield.textFont = "Arial";<br /> ofield.display = display.noView; //This is hidden on the page but printable to make viewable change to display.visible or noVie<br /> ofield.readonly = true;<br /> }<br /> }<br /><br /> /* Create a date object containing the current date. */<br /> var d1 = new Date();<br /> /* num contains the numeric representation of the current date. */<br /> var num = d1.valueOf();<br /> /* Add thirteen days to todays date, in milliseconds. */<br /> /* 1000 ms/sec, 60 sec/min, 60 min/hour, 24 hours/day, 14 days */<br /> num += 1000 * 60 * 60 * 24 * 14;<br /> /* Create our new date, 14 days ahead of the current date. */<br /> var d2 = new Date(num);<br /> /* var d3 = d2.valueOf(); */<br /> // Add Field to Document Script<br /> var strJS = 'var ofield = this.getField("DocMessage");';<br /> strJS += 'ofield.value = "Intellectual Property of Hematech - CONFIDENTIAL. ";';<br /> strJS += 'ofield.value += " Document expires on ' + util.printd("ddmmmyyyy", d2) + '.";'; <br /> this.addScript("AddMessage",strJS);
    DimitriM
    Inspiring
    January 6, 2006
    Dave,

    Did you test this in the JS debugger? I did and found the following problem with your code in the last line-

    strJS += 'ofield.value += " Document expires on " + util.printd("ddmmmyyyy", d2) + ".";';

    The key problem is that in the original script the printd statement used "new Date()". In the new code you replaced this with "d2" which is undefined in the document so that entire line never executes and should throw an exception that you would see in the debugger. Since you are using an "expiration date" in the document you dont have to recalculate the date every time with a script, i.e., you dont need to add a script to the document, just the value.

    In order for the printd statement to excute you need to terminate the single quote from the beginning of the line so the new line should look like this-

    strJS += 'ofield.value += " Document expires on ' + util.printd("ddmmmyyyy", d2) + '.";';

    Let us know if that fixes the problem. You should try the individual lines in the debugger in Acrobat (access it with Ctrl J)- this is an indispensable tool for working with JS in Acrobat.
    Good luck!

    Dimitri Munkirs
    Participating Frequently
    January 6, 2006
    Bump - anyone?