Copy link to clipboard
Copied
Here's a simple Javascript for-loop in Acrobat console. It works in any other console except Acrobat.
Any idea why?
I have also tried the following and still get undefined.
for(let i = 0; i < 10; i++){
console.log(i);
};
Copy link to clipboard
Copied
++ 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:
- https://answers.acrobatusers.com/Need-server-side-script-data-exchange-PDF-forms-data-q293094.aspx
- https://answers.acrobatusers.com/Server-side-scripting-q17412.aspx
- https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var
- https://www.tutorialspoint.com/difference-between-var-and-let-in-javascript
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.
Copy link to clipboard
Copied
Did you select the full code before executing it?
Copy link to clipboard
Copied
Use 'var' instead of 'let' and use 'println' instead of 'log'.
for(var i = 0; i < 10; i++){
console.println(i);
};
Copy link to clipboard
Copied
++ 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:
- https://answers.acrobatusers.com/Need-server-side-script-data-exchange-PDF-forms-data-q293094.aspx
- https://answers.acrobatusers.com/Server-side-scripting-q17412.aspx
- https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var
- https://www.tutorialspoint.com/difference-between-var-and-let-in-javascript
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.
Copy link to clipboard
Copied
Here's the error I got.
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.
Copy link to clipboard
Copied
Did you select the full code before executing it?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
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.

