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

Auto populate a PDF with current year to next year using Javascript

New Here ,
Jan 13, 2017 Jan 13, 2017

is it possible to auto populate a PDF with the last 2 digits 20__ - 20__ with current year to next year every time I open up a PDF page? example 2017-2018?

TOPICS
Acrobat SDK and JavaScript
2.3K
Translate
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 13, 2017 Jan 13, 2017

You need to add another field and then increment the year value by 1.

A possible script is:

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

f.value = util.printd("yy", new Date());

f = this.getField("NextYear");

f.value = Number(util.printd("yy", new Date())) + 1;

For this to work one needs to be careful about the type of variables, that is , there is a significant difference between a String variable and a Number variable and how the "+" operator works.

Translate
Community Expert ,
Jan 13, 2017 Jan 13, 2017

You can use this code to populate the whole thing into a field:

var now = new Date();

this.getField("Years").value = "" + now.getFullYear() "-" + (now.getFullYear()+1);

Translate
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
New Here ,
Jan 13, 2017 Jan 13, 2017

I get a syntaxError: missing ; before statement 2: at line 3 when I put this in the JavaScript Editor. How do I correct that? Thanks for your help.

Translate
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 13, 2017 Jan 13, 2017

What exact code did you use?

Translate
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
New Here ,
Jan 13, 2017 Jan 13, 2017

I copied & pasted your code exactly into my Acrobat Javascript editor but it returned with that syntaxError:

"missing ; before statement 2: at line 3"

so the code didn't work.

Translate
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
LEGEND ,
Jan 13, 2017 Jan 13, 2017

There was a missing "+" in the second line of the code.

var now = new Date();

this.getField("Years").value = "" + now.getFullYear() + "-" + (now.getFullYear()+1);

Another document level script could be:

var now = new Date();

this.getField("Years").value = (now.getFullYear()).concat("-", (now.getFullYear() +1));

You may need to change the field name to match your form.

Translate
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
New Here ,
Jan 13, 2017 Jan 13, 2017

That didn't work...

I have this JavaScript on Page Properties:

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

f.value = util.printd("yy", new Date());

so when the page pull up "20__ to 20__" comes up auto populated as "2017 to 20__".

Any way to add another script onto the above so it will show 2017 to 2018?

Translate
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
LEGEND ,
Jan 13, 2017 Jan 13, 2017

You need to add another field and then increment the year value by 1.

A possible script is:

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

f.value = util.printd("yy", new Date());

f = this.getField("NextYear");

f.value = Number(util.printd("yy", new Date())) + 1;

For this to work one needs to be careful about the type of variables, that is , there is a significant difference between a String variable and a Number variable and how the "+" operator works.

Translate
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
New Here ,
Jan 13, 2017 Jan 13, 2017
LATEST

That totally worked! Thank you gkaiseril and try67 for your help!

Translate
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