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

Why do I keep getting undefined in Acrobat Javascript console?

Enthusiast ,
Dec 21, 2021 Dec 21, 2021

Here's a simple Javascript for-loop in Acrobat console. It works in any other console except Acrobat.

2Charlie_0-1640131951930.pngexpand image

Any idea why?

 

I have also tried the following and still get undefined.

for(let i = 0; i < 10; i++){
 console.log(i);
};

 

TOPICS
JavaScript
3.6K
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
2 ACCEPTED SOLUTIONS
Community Expert ,
Dec 22, 2021 Dec 22, 2021

++ Adding to the discussion,

 

Would you mind describing what where you trying to achieve and why are you trying to declare a variable using "let"?

 

After much reading, I am almost convinced that you can't declare variables using "let" with  Acrobat JavaScript.

 

As illustrated by NesaNurani, when you script with an Acrobat PDF you must use "var" to declare variables. In addition, if you also write the same line of code and omit "var" when you express the for-loop function (shown in the example below), Acrobat JavaScript will automatically assume the variable declaration for the "i" indicator:


for( i = 0; i < 10; i++){
console.println(i);
};


In both instances (NesaNurani's correct example and the example illustrated above), the "i" indicator is initialized  as a global variable inside of the document that you're working with. Both examples will execute the same results.

 

Seems to me like you were testing this script to work around something in Acrobat like a server-side script to test with web browsers, for example (in which case is not directly supported in Acrobat).

See if these additional discussions are relevant:

 


As for "console.log", which is used to write or display data to the browser console, the standard output in Acrobat JavaScript is expressed with "console.println" instead. Otherwise, if successful, the Acrobat program will produce the write operation of the data transfer silently.

View solution in original post

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 ,
Dec 23, 2021 Dec 23, 2021

Did you select the full code before executing it?

View solution in original post

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 ,
Dec 21, 2021 Dec 21, 2021

Use 'var' instead of 'let' and use 'println' instead of 'log'.

for(var i = 0; i < 10; i++){
console.println(i);
};

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 ,
Dec 22, 2021 Dec 22, 2021

++ Adding to the discussion,

 

Would you mind describing what where you trying to achieve and why are you trying to declare a variable using "let"?

 

After much reading, I am almost convinced that you can't declare variables using "let" with  Acrobat JavaScript.

 

As illustrated by NesaNurani, when you script with an Acrobat PDF you must use "var" to declare variables. In addition, if you also write the same line of code and omit "var" when you express the for-loop function (shown in the example below), Acrobat JavaScript will automatically assume the variable declaration for the "i" indicator:


for( i = 0; i < 10; i++){
console.println(i);
};


In both instances (NesaNurani's correct example and the example illustrated above), the "i" indicator is initialized  as a global variable inside of the document that you're working with. Both examples will execute the same results.

 

Seems to me like you were testing this script to work around something in Acrobat like a server-side script to test with web browsers, for example (in which case is not directly supported in Acrobat).

See if these additional discussions are relevant:

 


As for "console.log", which is used to write or display data to the browser console, the standard output in Acrobat JavaScript is expressed with "console.println" instead. Otherwise, if successful, the Acrobat program will produce the write operation of the data transfer silently.

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
Enthusiast ,
Dec 23, 2021 Dec 23, 2021

Here's the error I got.

2Charlie_1-1640271128727.pngexpand image

I'm testing the script to rename all the field names in the PDF I have opened. At this point, I'm not even getting anything from the currently open PDF yet and it's giving me this error or undefined value. I have like 100 duplicate checkbox names that I want to make it unique.

 

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 ,
Dec 23, 2021 Dec 23, 2021

Did you select the full code before executing it?

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 ,
Dec 23, 2021 Dec 23, 2021

PS. You can't use a script to directly rename fields, if that's what you're trying to do... You will have to remove them, then add new ones with the new names in the same spot.

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
Enthusiast ,
Dec 23, 2021 Dec 23, 2021
LATEST

@try67 , that was it! I did not select all the code. I saw a video saying that all I have to do is put my cursor at the last line and hit ctrl+enter. Well, turned out to be incorrect. Thanks!

 

For doing what I wanted to do, I didn't know that I can't simply rename the field name. Thank you so much for the tip.

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 ,
Dec 22, 2021 Dec 22, 2021

Just to add to the correct replies made above: "undefined" is not an error. The opposite, actually. It just means the code finished executing correctly, without throwing any errors or returning any values.

The issues with your code cause it to not actually do anything, but it is running.

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