Skip to main content
October 6, 2010
Answered

Add Curent Date & Time to PDF when Printed

  • October 6, 2010
  • 1 reply
  • 42332 views

This is my first post so please excuse any ignorance.

Basically we are in the process of creating electronic copies of SOP’s and putting them on our file server for all staff to access. Due to regulations, when these SOP’s are printed they would need to have the current date and time in which the SOP was printed, ideally like a watermark faded diagonally across the page but even if it was something on the bottom or top this would also do the trick.

It should read "Only Valid on Date of Printing - <Current Date><Current Time>

Could somebody please tell me if something like this is possible by incorporating JavaScript into the PDF.

Any assistance I would be grateful.

Thanks.

Keith.

    This topic has been closed for replies.
    Correct answer gkaiseril

    You need to add a form field to PDF and then you will need to add a document action script for the "Will Print" action that adds any text and date and time data as needed. There are a number of post about how to do this, you can use the search box to find them.

    1 reply

    gkaiserilCorrect answer
    Inspiring
    October 6, 2010

    You need to add a form field to PDF and then you will need to add a document action script for the "Will Print" action that adds any text and date and time data as needed. There are a number of post about how to do this, you can use the search box to find them.

    October 7, 2010

    GKaiseril

    Thank you so much, I got it and it worked perfect courtesy of "hardDriver2". For anybody else who is looking to do this see below.

    ---------------------------------------------

    // Description: This script adds a text field to the left side of all

    // pages. It also sets a doc level JavaScript that executes when the

    // document is printed and fills the field with date.

    // Add field to each page of form

    var inch = 72;

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

        // create rectangle quads for field

        var aRect = this.getPageBox( {nPage: p} );

        aRect[0] = 0.0;

        aRect[1] = 0.0;

        aRect[2] = 26.0;

        aRect[3] = 792;

        // now construct text field to fill with date information

        var f = this.addField("PrintField", "text", p, aRect )

        f.delay = true;

        f.textSize = 16;

        f.textFont = font.HelvB;

        f.textColor = color.black;

        f.alignment = "center";

        f.readonly = true;

        f.print = true;

        f.hidden = true;

        f.delay = false;

    }

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

        // rotate the text 

        var f = this.getField("PrintField." + p)

        f.rotation = 90;

        }

    // Adds a doc level script to populate text field when doc prints from

    // Acrobat or the free Adobe Reader. Note: the '\r' character represents a

    // carriage return.

    var myWillPrintScript = 'var f = this.getField("PrintField"); \r' + 'f.hidden = false; \r' + 'var d = new Date(); \r' + 'var year = d.getYear()+1900; \r' + 'f.value = "THIS DOCUMENT ONLY EFFECTIVE ON " + (d.getMonth()+1) + "/" + d.getDate() + "/" + year; \r'

    var myDidPrintScript = 'var f = this.getField("PrintField"); \r'

                               + 'f.value = ""; \r'

                               + 'f.hidden = true; \r'

                   

    // Now set the scripts to execute on the Will/Did Print events.

    this.setAction("WillPrint", myWillPrintScript);

    this.setAction("DidPrint", myDidPrintScript);

    ---------------------------------------------------------

    Participant
    August 13, 2012

    Realize this thread is old but is answering my problem EXCEPT I don't see how to add the time stamp.  I have the code listed by kmcavinue20rrf working but it doesn't show the tume.  If I try to change by adding the code by GKaiseril, I get an error syntaxerror unterminated string literal for the last line of the code.  (this.getField(sNoticeField).value = sText; // set the field value)

    Below is how I have the code with the change,  did I insert the code incorrectly?  I have not worked with programming Acrobat previously so would appreciate any assistance getting the date into this stamp.  Otherwide the original code is perfect!

    // Description: This script adds a text field to the left side of all

    // pages. It also sets a doc level JavaScript that executes when the

    // document is printed and fills the field with date.

    // Add field to each page of form

    var inch = 72;

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

        // create rectangle quads for field

        var aRect = this.getPageBox( {nPage: p} );

        aRect[0] = 0.0;

        aRect[1] = 0.0;

        aRect[2] = 26.0;

        aRect[3] = 792;

        // now construct text field to fill with date information

        var f = this.addField("PrintField", "text", p, aRect )

        f.delay = true;

        f.textSize = 16;

        f.textFont = font.HelvB;

        f.textColor = color.black;

        f.alignment = "center";

        f.readonly = true;

        f.print = true;

        f.hidden = true;

        f.delay = false;

    }

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

        // rotate the text

        var f = this.getField("PrintField." + p)

        f.rotation = 90;

        }

    var sNoticeField = "PrintField"; // name of field for printed notice

    var oNow = new Date(); // get the JS date time object

    // build format string to display

    var sText =  "Printed on "; // date lead in text

    sText += util.printd("mmmm d, yyyy", oNow); // formatted date string

    sText += ' at '; // time lead in

    sText += util.printd("h:MM tt', oNow); // formatted time string

    this.getField(sNoticeField).value = sText; // set the field value

                  

    // Now set the scripts to execute on the Will/Did Print events.

    this.setAction("WillPrint", myWillPrintScript);

    this.setAction("DidPrint", myDidPrintScript);

    Thanks

    Sue


    Found the answer here:  http://forums.adobe.com/message/1097488