Skip to main content
Inspiring
November 21, 2023
Answered

timestamp Java Code

  • November 21, 2023
  • 5 replies
  • 6001 views

Hi Community,

 

Is there a Java code I can use in Adobe Acobat where it automatically puts the date and time.  I am not connected to a server.  The document is not a form. Nor do I want to use a form field. 

 

I am tested some code in the Java debugger it works, but it does not work when I go to Tools use JavaScript, then document java strings and create and use the script there. 

 

Then when I add the code to a document action it still does not work on the document.

 

My question is what java code can I used to create a date and timestamp and I want it to appear on every page of the document.  For example, if I have a ten page document I want the date for sure and a timestamp to be on all ten pages of the document.  Where will I code this at in Adobe Acrobat so it can be on every page of the document.

 

I do not have a digital certificate.  

 

Can I perform a date and time stamp without having to use a certificate, creating a form, or using a timestamp server.  Can it be done with Java Code?  Do you know what code to use and where would I put the code in Adobe Acobate?  Thanks. 

 

 

This topic has been closed for replies.
Correct answer InezS

Hi Nurani, 

 

It is me again, the code below I found on another website and it is for Java script, however it does not work in the adobde acrobate java script. when the code is run on the website it works.  The answer will be ideal for what I am looking for I may just have to forget about the time stamp.  The code is below, let me know what you think and why it is not working in adobe java script.  Can it be added to the code you gave me? or do I need to remove the console part?

 

This is the example that should appear at the bottom or top of the page:

 

December 18, 2023, 11:00 p. m. central time, Thank you 352.

 

or THANK YOU 352.

 

Thanks 

 

var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log('Day of year: ' + day);

Hi Community, I have another question.  here is my code below.  I am trying to get the previous codes to work with some code I saw online or in a book.  the orginal code from the book or online is below.

 

{ var r = [200, 200, 400, 300]; var i = this.pageNum;

var f = this.addField(String("completeDate."+i),"text",i,r); f.textSize = 10; f.alignment = "right"; f.textColor = color.blue; f.fillColor = color.transparent; f.textfont = font.HelvB; f.strokeColor = color.transparent; f.value = String("This page was reviewed on: " + util.printd("mmm dd, yyyy", new Date())); f.readonly = true; }

 

I was able to get the correct answer.  Thank you Adobe community. 

 

5 replies

Nesa Nurani
Community Expert
Community Expert
November 22, 2023

You can use this script to place a timestamp at the bottom of every page as a comment, which will be locked from editing.

You can change time format, text properties as you wish:

var currentDate = new Date();
var timestamp = currentDate.toLocaleString();

for (var i = 0; i < this.numPages; i++) {
var annot = this.addAnnot({
    page: i,
    type: "FreeText",
    textFont: font.Helv,
    textSize: 10,
    contents: timestamp,
    rect: [225, 13, 375, 49],
    width: 1,
    alignment: 1,
    strokeColor: color.white,
    lineWidth: 0,
    lock: true
    });
}
InezSAuthor
Inspiring
November 22, 2023

Hi Nurani,

 

Thanks for the script I will try it over the weekend and let you know how it turns out.  Happy Holidays. 

JR Boulay
Community Expert
Community Expert
November 21, 2023

Other users must have Acrobat Pro to be able to edit Header & Footer.

 

You can restrict document modification without prohibiting comments.

 

Acrobate du PDF, InDesigner et Photoshopographe
InezSAuthor
Inspiring
November 22, 2023

Thank you, but did want to set a password.  Sometimes the things we want to achieve may not be possible and this just may be one of those things.  No password because there can possible be many users.  Thanks for the suggestions. 

JR Boulay
Community Expert
Community Expert
November 21, 2023

Fortunately, you don't need JavaScript nor form fields to add a date to every page.
That's what the Header & Footer function is for, and it allows easy updates.

 

 

Acrobate du PDF, InDesigner et Photoshopographe
InezSAuthor
Inspiring
November 21, 2023

Yes, I thought about that but I don't want the date to be edible by other users.  Yes, I know I can make it a read only, however the other users of the document may need to make notations on it.  Thanks. 

try67
Community Expert
Community Expert
November 21, 2023

You can add it as a watermark (using the addWatermarkFromText from text command), but if you want to update it later on it will overlap with the previous text you added. That's why using a field is better, as it's easier to update it.

Any specific reason you don't want to use a form field for this?

InezSAuthor
Inspiring
November 21, 2023

Yes, I thought about the watermark, but want the date or time stamp to be at the top or bottom of the document and do not want it to be edible by other users.  I may have to reconsider using a field.  Thanks. 

try67
Community Expert
Community Expert
November 21, 2023

The question is when do you want it to update, if at all. Should it update each time the file opened? Saved? Printed? Something else?

Bernd Alheit
Community Expert
Community Expert
November 21, 2023

Info: Acrobat uses JavaScript, not Java.

InezSAuthor
Inspiring
November 21, 2023

I meant JavaScript. Thanks. 🙂