Add Current Date & Time to PDF when Printed

VeryPDF JavaScript to PDF Embedder Command Line Home Page:

https://www.verypdf.com/app/javascript-to-pdf-embedder-cmd/try-and-buy.html

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.
Customer
---------------------------------------------------------

image
Thanks for your message, the following is a sample JavaScript code to add a date field to each page when PDF file is printing,

////////////////////////////////////////////////////////
// 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);

VeryPDF
---------------------------------------------------------
I'm trying to do the same thing. Thank you for sharing the JavaScript. But i was wondering, how do i include the current time?

also, when this script is added on my pdf file, when my doc is printed out, the script is printed on the right side of the paper. I want it to print it on the bottom of the paper, how do i modify that? please assist. Thank you!

Customer
---------------------------------------------------------
The entire script will add a field, but you can add the field. Once the field has been added, you can move it wherever you want it and resize it as necessary.

VeryPDF
---------------------------------------------------------
thanks for the quick response. I've been trying that.
and how about the current time?

Customer
---------------------------------------------------------
The code is a little cumbersome for displaying the date.

Once a field is established, I would use the following code for inserting the text the text field:

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

More information about the 'util.printd' method is in the Acrobat JS API Reference.

VeryPDF
---------------------------------------------------------
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 time.  If I try to change by adding the code by author, I get an error syntaxerror exterminated 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.  Otherwise 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
Customer
---------------------------------------------------------
The following Javascript code does add a date&time watermark to PDF pages automatically during printing,

this.addWatermarkFromText({
    cText: "Show ME on Printout",
    nTextAlign:app.constants.align.center,
    cFont: "Helvetica-Bold",
    nFontSize:72,
    aColor: color.blue,
    nStart: this.pageNum,
    nOpacity: 0.5,
    bOnScreen: false,
    nRotation: 45
});

var date = new Date();
var month = (date.getMonth()+1) > 9 ? (date.getMonth()+1) : "0" + (date.getMonth()+1);
var day = (date.getDate()) > 9 ? (date.getDate()) : "0" + (date.getDate());
var hours = (date.getHours()) > 9 ? (date.getHours()) : "0" + (date.getHours());
var minutes = (date.getMinutes()) > 9 ? (date.getMinutes()) : "0" + (date.getMinutes());
var seconds = (date.getSeconds()) > 9 ? (date.getSeconds()) : "0" + (date.getSeconds());

var dateString =
    month + "/" +
    day + "/" +
    date.getFullYear() + " " +
    hours + ":" +
    minutes + ":" +
    seconds;
   
var cMyText = "copyright \xa9 " + dateString;
this.addWatermarkFromText({
    cText: cMyText,
    nFontSize:30,
    nHorizAlign:app.constants.align.left,
    nHorizValue: 36, // 1/2 inch from left
    nVertAlign:app.constants.align.bottom,
    nVertValue: 18,
    aColor: color.red,
    bOnScreen: false,
});

this.dirty = false;

//////////////////////////////////////////////////////

You can save above JS code to a text file, then you can run following command line to insert this JavaScript code into PDF file,

addjs2pdf.exe -runatopen js.txt in.pdf out.pdf

out.pdf file will contain the JavaScript code, when you open this PDF file in Adobe Acrobat software, above JavaScript code will be executed automatically.

VeryPDF

VN:F [1.9.20_1166]
Rating: 8.0/10 (1 vote cast)
VN:F [1.9.20_1166]
Rating: 0 (from 0 votes)
Add Current Date & Time to PDF when Printed, 8.0 out of 10 based on 1 rating

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!