• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I get the current date to show in the document and also print?

Community Beginner ,
Jan 19, 2018 Jan 19, 2018

Copy link to clipboard

Copied

Hello! This is lengthy but I wanted to try to include all relevant information. I already appreciate that you're here and I hope you can help.

I'm on Windows 7 using Acrobat Pro DC.

I've been Googling all day and trying different things and I can't seem to figure it out.

I want the form to show the current date when the file is opened.

And I also want the current date to print exactly how you see it in the file.

I need to know where to put the JS code (and to verify the one I'm using is correct).

My method:

Open the document

Tools

Prepare form

Place a text box with Text inside

Click All Properties

Now here is where my findings branch off...

1) One method tells me to click on Format then choose Date from the dropdown, then I choose my style, mmmm d, yyyy

I don't know what to do from there.

2) Another method tells me to click on Format then choose Calculate, choose Custom Calculation Script, Edit, then paste JavaScript code.

I've found variations of this code, but this is the one I used:

var myfield=getField("Text");

var date = new Date();

date=util.printd("mmmm d, yyyy", date);

a) And another suggestion was to goto Tools, JavaScript, Document JavaScripts, Add start, use:

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

I don't know which method is more "correct" or if I'm even using the correct JS is the correct places.

I want to specify that I want the current date to show on the page when the document is opened. Currently, I cannot see the date on the page.

Any guidance would be greatly appreciated!!

TOPICS
Acrobat SDK and JavaScript , Windows

Views

1.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 19, 2018 Jan 19, 2018

If you want today's date to appear on a form or in a PDF when it is opened the easiest way is to use a form field and a script to populate the form when the PDF is opened. The form field needs to be set to "Read Only" unless you want the user to override the value of the field. To have the field display and print one sets the filed's display property to "Display". If you are using a script to populate the field I would let the script set the format for the displayed field so this field works sim

...

Votes

Translate

Translate
LEGEND ,
Jan 19, 2018 Jan 19, 2018

Copy link to clipboard

Copied

If you want today's date to appear on a form or in a PDF when it is opened the easiest way is to use a form field and a script to populate the form when the PDF is opened. The form field needs to be set to "Read Only" unless you want the user to override the value of the field. To have the field display and print one sets the filed's display property to "Display". If you are using a script to populate the field I would let the script set the format for the displayed field so this field works similar to date fields that have user entry.

As for using the "Custom Calculation JavaScript", do not use it. This will only work if you have at least one other field with a calculation. This is needed to trigger the recalculation of the form. Also using this approach will cause the auto fill date field to recalculate each time any calculation on the form is triggered. This is a waste of time and could under some circumstances cause the field's value to change.

I prefer to use the document level script since this script only runs when the form opened and not a second time.

There are some possible situations that you do not discuss. What happens if the form is reset or saved, closed, and reopened?

Resetting the form will clear the field and not repopulate it.

For the save, close and reopen the date field will change.

So I would use a script that includes use of the default value of the form to hold the date value when the form was first opened and is tested to see if there is no value before updating the value of the field and setting the default value of the field. In this manner the date field will only populate if the field has not be populated and will restore the date the form was first opened if the form was reset. Also it is possible to edit the form and then remove the default value from the field before saving the updated form for distribution to others and have the date field properly update.

var 0Date = this.getField("Today");

if(oDate.defraultValue == "")

{

oDate.defaultValue = util.printd("mmmm d, yyyy", new Date());

oDate.value = oDate.defaultValue;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 22, 2018 Jan 22, 2018

Copy link to clipboard

Copied

Thank you so much for your detailed response.

I followed your directions and added the script in as document level and also added this.dirty = false; as suggested by @JR_Boulay so it looks like this:

var oDate = this.getField("Today");

if(oDate.defaultValue == "") {

oDate.defaultValue = util.printd("mmmm d, yyyy", new Date());

oDate.value = oDate.defaultValue;

this.dirty = false;

}

1) There was a typo in the suggested code at the first variable name. It was 0Date and I changed it to oDate.

2) There was a typo in the if statement. It was defraultValue and I changed it to defaultValue.

It works!!

Thank you both so much for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 30, 2018 Jan 30, 2018

Copy link to clipboard

Copied

I have another question about this.

The situation is that this PDF is set as a link on a website. When someone clicks on the link, the PDF opens in the browser window. When this happens, the date field is blank. Is there a way to set the date to show a default date for situations like this?

Note: when the same file is downloaded and opened in Acrobat, the date shows the current date, as it should.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2018 Jan 30, 2018

Copy link to clipboard

Copied

LATEST

No, if the PDF viewer used doesn't execute the script there's nothing that can be done about it.

The only alternative is to have a back-end service on your server that updates the date in the PDF each day, or each time it is downloaded.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2018 Jan 20, 2018

Copy link to clipboard

Copied

I would just add that line after gkaiseril's script:

this.dirty = false;

So, an user that just opens the document and close it will not be prompted to save.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines