Script that list form feilds

Participant ,
Dec 08, 2020

Copy link to clipboard

Copied

Hello all, I would like to list all the fillable fields in a PDF in an array for each page. I found a page that seems to say its script will do just that. I cannot get it to work. Could someone take a look and tell me what is wrong. In the function that is created should I be inserting my document name? If anyone has a better way to do this I will appreciate that as well.

 

The page that offers a script: Page at Acrobat Library 

 

function getPageFields(doc, p) {
var fields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.getNthFieldName(i));
if ((typeof f.page=="number" && f.page==p) || (typeof f.page!="number" && f.page.indexOf(p)>-1)) fields.push(f.name);
}
return fields;
}

 If my results are listed in the console it is fine. I want the listing to refer to them elsewhere. Thanks for any help.

I see now... I did not know I had to adjust the function for the page number. I thought it would loop through every page. I get it, and this is very helpful.  I am new to Acrobat scripting so I want to learn more because I see how powerful it is. Thank you so much.

TOPICS
Acrobat SDK and JavaScript, How to

Views

70

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Script that list form feilds

Participant ,
Dec 08, 2020

Copy link to clipboard

Copied

Hello all, I would like to list all the fillable fields in a PDF in an array for each page. I found a page that seems to say its script will do just that. I cannot get it to work. Could someone take a look and tell me what is wrong. In the function that is created should I be inserting my document name? If anyone has a better way to do this I will appreciate that as well.

 

The page that offers a script: Page at Acrobat Library 

 

function getPageFields(doc, p) {
var fields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.getNthFieldName(i));
if ((typeof f.page=="number" && f.page==p) || (typeof f.page!="number" && f.page.indexOf(p)>-1)) fields.push(f.name);
}
return fields;
}

 If my results are listed in the console it is fine. I want the listing to refer to them elsewhere. Thanks for any help.

I see now... I did not know I had to adjust the function for the page number. I thought it would loop through every page. I get it, and this is very helpful.  I am new to Acrobat scripting so I want to learn more because I see how powerful it is. Thank you so much.

TOPICS
Acrobat SDK and JavaScript, How to

Views

71

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 08, 2020 0
Adobe Community Professional ,
Dec 08, 2020

Copy link to clipboard

Copied

This is a document level script.

 

The first part of the script is declaring a function, which is  what you currently posted from that link.

 

The second part of the script has to call this function. You need to call the function using this other script:

 

getPageFields(this, 0);

 

In my example slides below I  used the JavaScript tool --->> Document Scripts to create the function. 

 

DOCUMENT JAVASCRIPS.png

 

 

 

Then I added a button with a Mouse-up event to call the function and report back to the console with this line:

 

console.println(getPageFields(this, 0));

 

The results look like shown below when I call the function from the button and open the console:

 DOCUMENT JAVASCRIPS2.png

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 08, 2020 0
Participant ,
Dec 08, 2020

Copy link to clipboard

Copied

Hi, thanks for the reply. I think I did the first part correctly. However, as for creating a button I used the tools on the right and searched for button. I then set the button up, I choose All Properties and then Run a JavaScript -> Add I put the

console.println(getPageFields(this, 0));

there. When I press it with the console open I just get the default button name in the console. In my case "Button1". I think I am not setting up the button correctly.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 08, 2020 0
Adobe Community Professional ,
Dec 08, 2020

Copy link to clipboard

Copied

How many form fields are you expecting to see listed?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 08, 2020 0
Participant ,
Dec 08, 2020

Copy link to clipboard

Copied

I see now... I did not know I had to adjust the function for the page number. I thought it would loop through every page. I get it, and this is very helpful.  I am new to Acrobat scripting so I want to learn more because I see how powerful it is. Thank you so much.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 08, 2020 0
Adobe Community Professional ,
Dec 08, 2020

Copy link to clipboard

Copied

You're welcome.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 08, 2020 0
Participant ,
Dec 11, 2020

Copy link to clipboard

Copied

I definitely need to learn more about how this all works. I see simple programming constructs that I know something about are different here. For example, if I stack up the functions that return the fields but manually, they return what I want.

console.println("Page 2: " + getPageFields(this, 1)  + "Page 5: " + getPageFields(this, 6)  + "Page 11: " + getPageFields(this, 12)  + "Page 20: " + getPageFields(this, 19) );
// This returns the fields from page 2, 5, 11, and 21

However, if I do this with a loop it seems to need different syntax than I am used to.

var intTotalPages = this.numPages ;
for ( var i = 1; i < intTotalPages; i++ ) {
console.println( "Page " + (i + 1) + ":" + getPageFields(this, i));
}
This returns undefined every time.

Could someone explain what is different here about using a loop? Thanks

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 11, 2020 0
Most Valuable Participant ,
Dec 11, 2020

Copy link to clipboard

Copied

The syntax is the same. "undefined" doesn't mean there was an error, just the opposite. It means that the code completed running without errors and without returning any values. The issue is you're not seeing any output in the console, but in order to debug that we'll need to see the actual file with the full code.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 11, 2020 0
Participant ,
Dec 11, 2020

Copy link to clipboard

Copied

Here you go... this is an example of a document that is so lengthy it is good to have a list of fields so you do not miss anything. I would like to loop through and print all of them to the console. Optimally I would like to have them listed (organized) by page that they reside on. I have already inserted the JavaScript the way it was taught to me in the messages above. Thanks for any help/ideas.

http://pctechtv.com/extra/forum/adobe/201211ListFieldsJSHlp/gd17A.zip

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 11, 2020 0
Most Valuable Participant ,
Dec 11, 2020

Copy link to clipboard

Copied

Works just fine for me, using this code:

 

var intTotalPages = this.numPages ;
for ( var i = 1; i < intTotalPages; i++ ) {
console.println( "Page " + (i + 1) + ":" + getPageFields(this, i));
}

function getPageFields(doc, p) {
var fields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.getNthFieldName(i));
if ((typeof f.page=="number" && f.page==p) || (typeof f.page!="number" && f.page.indexOf(p)>-1)) fields.push(f.name);
}
return fields;
}

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 11, 2020 0
pctechtv LATEST
Participant ,
Dec 11, 2020

Copy link to clipboard

Copied

Thank you... it sure does. So maybe I was a little hasty when I posted it "programming constructs that I know something about are different here" I wrote that loop with what I know about JavaScript but what I did wrong is I tried to run it directly from the console. The reason I did this is because when I used the single console.print that worked directly in the console. Now I reverted back to using the button and like you said it works with that code. Note (all new to this like me) I also changed  the for loop to have a

var i = 1;

instead of the zero wich it should start with. I also did this because I coudl the get the other way to work so change it back. 🙂

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 11, 2020 1