These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Counting multiple occurrence of same field name.

pranabeshpanda
Registered: May 8 2009
Posts: 30
Answered

Hi,

Could anybody tell me, whether there is a method or technique to count multiple occurrence of same field name?

Ex:

I do have 12 fields in a form, which includes 2 fields having same name. But when I use the properties
var fCount = this.numFields; //numFields
it shows me only count 11, as it treats the duplicate filed names as one element.

Could anybody tell me how to fix this issue?

Thanks & Regards,
Pranabesh

Thanks & Regards
Pranabesh

My Product Information:
Acrobat Pro 9.0, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You should read the reference file. It explains how to access a specific "widget" using its index.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

pranabeshpanda
Registered: May 8 2009
Posts: 30
Hi,

Thanks a lot for the same.

Could you please give one example relating to the text fields?

Thanks
Pranabesh

Thanks & Regards
Pranabesh

try67
Expert
Registered: Oct 30 2008
Posts: 2398
this.getField("textFieldName.0");
this.getField("textFieldName.1");
this.getField("textFieldName.2");
etc.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

pranabeshpanda
Registered: May 8 2009
Posts: 30
Thank you very much for the code. :) it really worked some part of my code.
But I stuck again after that :(

Could you please help me on the below scenario?

Let me give you one example,

In a form there are some n number of fields. In that some fields are being repeated and some are now.

Ex:

textField1
textField2

textField3
textField3
textField3

textField4
textField5

So I want to count how many fields are there, which are being repeated.

NB : No one can guess how many fields are there in that form specifically, which are being repeated.

Thanks & Regards
Pranabesh

try67
Expert
Registered: Oct 30 2008
Posts: 2398
I didn't try this, but I think you can do it by creating a loop that goes from 0 to whatever (or just an infinite one), and put a try-catch around your getField command. If it fails, you exit the loop.
So something like this:

for (i=0; i>-1; i++) {
try {f=this.getField("textFieldName."+i);
// Do some things...
}
catch (e) {break;}
}

Again, I didn't try this so I can't guarantee it will work... The reference file doesn't specify what happens when getField fails. You can add this inside the loop, just in case:
if (f==null) break;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

pranabeshpanda
Registered: May 8 2009
Posts: 30
Hi,

I have tried your example...it worked fine with me :)
But when I am trying to apply that logic to the whole form, the code stuck :(

Below is my code;

var fCount = this.numFields;
var fName;
var field = 0;


for(field = 0; field < fCount; field++)
{
fName = this.getNthFieldName(field);
for(var i = 0; i > -1; i++){
try{
var fieldName = new Array();
fieldName[i] = this.getField(""+fName+"."+i);
}catch(e){break;}
}

// If I add the below lines, the code stuck.

if(fieldName.length > 2){
for(var j = 0; j > fieldName.length-1; j++){
fieldName[j].rotation = 90;
}
}
else if(fieldName.length == 2){
fieldName[0].rotation = 90;
}
}

If I am doing some mistake, then please point out that .....

Thanks & Regards
Pranabesh

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Sorry, but I don't understand what you're trying to do.

Also, this line of code doesn't make any sense. This condition is never met:
for(var j = 0; j > fieldName.length-1; j++){

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

pranabeshpanda
Registered: May 8 2009
Posts: 30
Hi,

Well this code is gonna rotate the text in the text box about 90 degree

Thing is that if you consider the below part ;

var fCount = this.numFields;
var fName;
var field = 0;


for(field = 0; field < fCount; field++)
{
fName = this.getNthFieldName(field);
for(var i = 0; i > -1; i++){
try{
var fieldName = new Array();
fieldName[i] = this.getField(""+fName+"."+i);
}catch(e){break;}
}
app.alert(fieldName.length);
}

and if I am gonna include only a single line of code to display the length of the fieldName array, even then its getting stuck :(
Forget about the rest of the part....If I am considering the above part, then this thing is happening.

Thanks & Regards
Pranabesh

pranabeshpanda
Registered: May 8 2009
Posts: 30
Well some amazing things are happening with the code.....

If I put a simple alert message after the following line;

fieldName[i] = this.getField(""+fName+"."+i);
app.alert("Field Name : " + fieldName[i].name + "Field Value : " + fieldName[i].value);

then the whole code executes properly.

But if I remove the same alert message, then the code is getting stuck.....

Do you think that an alert message will create this kinda amazing behavior?

Thanks & Regards
Pranabesh

try67
Expert
Registered: Oct 30 2008
Posts: 2398
I suspect the problem is with this line:
fieldName[i] = this.getField(""+fName+"."+i);

That is not how you add a new item into an array. You need to use a method like push().

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

pranabeshpanda
Registered: May 8 2009
Posts: 30
i have tried using the push() method to store the elements into the array.
But still the same issue. :(
I am just irritated with the behavior ....

Thanks & Regards
Pranabesh

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Take the array definition out of the loop. You shouldn't redeclare it with each iteration.
Also, you can't declare it as a local variable and then try to access it from outside of the local scope in the line:
app.alert(fieldName.length);

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

pranabeshpanda
Registered: May 8 2009
Posts: 30
I have tried executing like this;

First Time :

var fCount = this.numFields;
var fName;
var field = 0;
var fieldName = new Array();

for(field = 0; field < fCount; field++)
{
fName = this.getNthFieldName(field);
for(var i = 0; i > -1; i++){
try{
fieldName[i] = this.getField(""+fName+"."+i);
app.alert("Field Name : " + fieldName[i].name + "Field Value : " + fieldName[i].value);
}catch(e){break;}
}
app.alert("Field Length : " + fieldName.length);
}

Second Time :

var fCount = this.numFields;
var fName;
var field = 0;
var fieldName = null;

for(field = 0; field < fCount; field++)
{
fName = this.getNthFieldName(field);
fieldName = new Array();
for(var i = 0; i > -1; i++){
try{
fieldName.push(this.getField(""+fName+"."+i));
//app.alert("Field Name : " + fieldName[i].name + "Field Value : " + fieldName[i].value);
}catch(e){break;}
}
app.alert("Field Length : " + fieldName.length);
}

Both time its the same issue. If I remove the comment from first commenting line, then everything works fine.....

Actually the form is having some 247 fields all together. Do you think, the problem would be persist in the number of fields?

Thanks & Regards
Pranabesh

try67
Expert
Registered: Oct 30 2008
Posts: 2398
As I wrote to you in one of my earlier posts, I did not check the code and was not sure that Acrobat throws an error when it can't find the field. As it turns out, I was right. It just returns null. If you would have checked that earlier you could have saved yourself a lot of problems. Anyway, this code works:

var fCount = this.numFields;
var fieldName = null;

for(field = 0; field < fCount; field++)
{
fName = this.getNthFieldName(field);
fieldName = new Array();
for(var i = 0; i > -1; i++){
var f = this.getField(""+fName+"."+i)
if (f==null) break;
fieldName.push(f);
}
app.alert("Field Length : " + fieldName.length);
}

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

pranabeshpanda
Registered: May 8 2009
Posts: 30
Thank you very much for your support...
It really worked for me :)

You are The Genius :)

Thanks & Regards
Pranabesh