We help IT Professionals succeed at work.

JavaScript code in PDF

donnie91910
donnie91910 asked
on
High Priority
68 Views
Last Modified: 2020-02-22
I have a PDF with two text fields on it "Today" and "Tomorrow".  I am trying to have the form read the "Today" field and then display "Today" + 1 day in the "Tomorrow" field.  So, if the "Today" text field is 2/18/2020 then the "Tomorrow" text field will read 2/20/2020.  Here is the code I have so far:
this.addScript("init", "var d = new Date(); var f = this.getField(\"Today\"); if (f.value==\"\") f.value = d; var t = this.getField(\"Tomorrow\"); t.value = t.setDate(d+1);")
Comment
Watch Question

Author

Commented:
The problem that i am having is that the "Tomorrow" field is not populating.
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
do you get the Today value populated from your code?

Author

Commented:
yes. The "Today" field does get populated.
CERTIFIED EXPERT

Commented:
Hi,

Usually Javascript won't play well into PDF or not at all .
What do you use to generate the PDF?
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
test page : https://jsfiddle.net/p0bqjngs/

Also please note init doesn't run when the user type something (change) in Today
You should use Acrobat Events to run your code (when document open and when the user leave the Today field)

  var today = new Date();
  var tomorrow = new Date();

  var todayField = this.getField("Today");
  var tomorrowField = this.getField("Tomorrow");

  if(todayField.value == "") {
      todayField.value = (1+today.getMonth()) + "/" + today.getDate() + "/" + today.getFullYear();// MM/DD/YYYY
  }
  else {
      var txt = todayField.value.split("/");
      today = new Date(txt[2], txt[0]-1, txt[1]);
  }
  tomorrow.setTime(today.getTime()+(24*60*60*1000));
  tomorrowField.value = (1+tomorrow.getMonth()) + "/" + tomorrow.getDate() + "/" + tomorrow.getFullYear();

Open in new window


If you still want to try your method :

this.addScript("init",'var today=new Date;var tomorrow=new Date;var todayField=this.getField("Today");var tomorrowField=this.getField("Tomorrow");if(todayField.value=="")todayField.value=1+today.getMonth()+"/"+today.getDate()+"/"+today.getFullYear();else{var txt=todayField.value.split("/");today=new Date(txt[2],txt[0]-1,txt[1])}tomorrow.setTime(today.getTime()+24*60*60*1E3);tomorrowField.value=1+tomorrow.getMonth()+"/"+tomorrow.getDate()+"/"+tomorrow.getFullYear();');

Open in new window

Author

Commented:
leakim971 - the modified version of my code worked perfect.
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
good to hear!