These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Need text field with date first opened and then doesn't change

jeffchaskin
Registered: Sep 19 2008
Posts: 22

I have created a pdf certificate which is valid one year from the date received. I need the date to populate the first time the doc is opened and then not change. I have tried the javascript below in the page properties which gives the date perfectly. How can I make the date of that first event "stick" forever and not be user changeable?
var newdate = new Date();
var thisfieldis = this.getField("thedate");
var thedate = util.printd("d",newdate);
var themonth = util.printd("mmmm",newdate);
var theyear = util.printd("yyyy",newdate);
thisfieldis.value = themonth + " " + thedate + ", " + theyear ;

Thanks much

My Product Information:
Acrobat Pro 6.0, Macintosh
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
One thing you can do is before setting the field value, check to see if there is an existing value. If not, go ahead and set the value to the date string you've built. I would also set the field's defaultValue property to the same thing so it will survive a resetForm action.

Be aware that this can't be considered secure, as there are ways to change the field value, but it will prevent accidental changes or casual attempts to change it.

George
jeffchaskin
Registered: Sep 19 2008
Posts: 22
Thanks George. Can you put that in code I can cut and paste. I'm really not a coder, more of the designer. The code in my post was adapted from another forum answer.
Jeff
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
Try this:

var date_field = getField("thedate");
var date_string = util.printd("mmmm d, yyyy", new Date());

if (date_field.value === "") {
date_field.value = date_string;
date_field.defaultValue = date_string;
}

Where did you intend to place this code?

George
jeffchaskin
Registered: Sep 19 2008
Posts: 22
Thanks George,

Either in the page properties as javascript after page open action, or in the text field as a custom calculation script. I would then add security so the user can't modify. I'm in 6.0 Pro for Mac OS X so my options are limited.

Jeff
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
I would use a document level script, see [url=http://www.acrobatusers.com/tutorials/2007/js_document_scripts/]Entering Document Scripts[/url] by Thom Parker, that is only run upon openging the document and not every time the page is opened. You will have to save the form with the date date field empty.

George Kaiser

jeffchaskin
Registered: Sep 19 2008
Posts: 22
I'll try anything, but I can't write it. Also this may sound stupid, but how do I keep my creation of the field and script from populating the field and making the creation date the permanent date?

Jeff
jeffchaskin
Registered: Sep 19 2008
Posts: 22
George_Johnson wrote:
Try this:var date_field = getField("thedate");
var date_string = util.printd("mmmm d, yyyy", new Date());

if (date_field.value === "") {
date_field.value = date_string;
date_field.defaultValue = date_string;
}

Where did you intend to place this code?

George
I used this in the page properties with a 'page open' action. Set password security on and saved while still in advanced editing mode. Shows the date perfectly, but the date changes with the real time date, not static with the date first opened, ie. created on 9/22 and opened on another computer on 9/22 with Reader. Next day it changed to 9/23.

Jeff
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
I would use a document level script like George Johnson suggested:

var date_field = getField("thedate");
// update if date field is empty
if (date_field.value == "") {
// set displayed value
date_field.value = util.printd("mmmm d, yyyy", new Date());;
// set default value
date_field.defaultValue = date_field.value;
}

You should make the field read only and because you are formatting the date string you only need text formatting.

But when you save the form you will need to clear the date field's value and default value. You can do this by using the form field pane or editing the field before saving the form.

George Kaiser

jeffchaskin
Registered: Sep 19 2008
Posts: 22
Sorry to be a pest but...

I tried the above script at both the document and page level and it always returns the system date.
I am clearing the text field before saving by clearing the default value, and the field shows no data, only its filed name before saving.
I am then copying the doc to another computer which has its system date set one day ahead. When I open the file on that machine it displays the date as September 24, 2008. When I copy it back to the original machine it reads September 23, 2008.
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
If you do not save the document after the date field value gets set, then you will see the behavior you describe. Did you save it after opening it on the first machine?

George
jeffchaskin
Registered: Sep 19 2008
Posts: 22
No, I saved it after adding the script and clearing the text field default. I saved with password security on.
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
Part of the problem is I'm not sure what your intended workflow is. I expected that you would be creating a PDF to use as a master document. The date field of this master document would be blank and there would be no need for security. When you needed to create a new certificate, you would open this master document and the current date would get set in the text field. You would then save this modified document (with security, as a new document, separate from the master document) and the text form field would retain the date thereafter. Is what you want to do different from this?

George
jeffchaskin
Registered: Sep 19 2008
Posts: 22
Thanks for your patience George. I will have the master document on my server (with no date) and send a download link to the end user. When he downloads and opens the document, I wish it to then populate the system date and keep it from changing. The document is a certificate which expires 1 year from the issue (date first opened by user). So my master on the server is always blank, the users first open is the trigger.
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
I see...that's a different problem and will require a completely different approach.

You need to set the date field (or otherwise stamp the PDF) before the file is downloaded, which means creating a new file on the server. This is possible, but not with JavaScript code inside the PDF.

George
jeffchaskin
Registered: Sep 19 2008
Posts: 22
At the risk of getting in way over my head, how do I do that? My desire is that no matter how many times they wish to print the certificate, or when, it will always have the same date - which can either be the download date or the first open/first printed date.
George_Johnson
Online
Expert
Registered: Jul 6 2008
Posts: 1876
There are a number of server-side tools available that can stamp a PDF or fill fields, such as those from [url=http://www.appligent.com/products/products.php]Appligent[/url], but this might be more than you bargained for. You may have to consider a different delivery method.

George