Instead of rename many fields -> Add and remove fields

Community Beginner ,
Jun 05, 2020 Jun 05, 2020

Copy link to clipboard

Copied

Hello everybody,
I created a form from an Excel sheet. Acrobat has badly named many fields. I don't want to rename them all by hand. Instead I want to "read" the original fields, create a new one with a different name in the same position and then delete the old one.

I would like to test this in 12 fields (there is one on each of the 12 pages of the form). When creating the form, they were named as follows: Name / Name_2 / Name_3 / ... / Name_12.
To do this, I tried the following code in the console:

 

for(var i = 0; i < this.numFields; i++){
	var fName = this.getNthFieldName(i);
		
	if(fName.indexOf("Name") !== -1){
		var f = this.getField(fName);
		var fCoords = [];
		var fPage = (f.page + 1);

		fCoords = f.rect;
		this.addField(("Name." + fPage), "text", f.page, fCoords);
		this.removeField(f.name);
		fCoords.length = 0;
	}
}

 

It works as it should on 10 pages.

Only on page 1 (.page 0 / .name: "Name") and on page 10 (.page 9 / .name: "Name_10") not. On page 1, the field is only deleted, but no new one is created. Nothing happens on page 10. The Name_10 field remains and no new field is created.
I don't understand why this happens and would be very happy if someone can explain it to me and how I can solve the problem or do it better.

 

Thanks in advance for your help.

TOPICS
Acrobat SDK and JavaScript, Create PDFs, Edit and convert PDFs, PDF forms

Views

289

Likes

translate

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
community guidelines
Most Valuable Participant ,
Jun 05, 2020 Jun 05, 2020

Copy link to clipboard

Copied

Don't loop in this way. You are deleting something from the list you are processing. Every programmer does this at some time, it is like sitting on a tree branch you are sawing off. Normally, I'd say you must go BACKWARDS through the list of fields, not forwards. But even that doesn't seem safe because nowhere is there a rule that the new added field will be at the end of the list. I suggest that after each replace, you start the whole scan again. So use the loop to find a candidate, don't make the change inside the loop.

Likes

translate

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
community guidelines
Adobe Community Professional ,
Jun 05, 2020 Jun 05, 2020

Copy link to clipboard

Copied

First create a array or list of the fields. Then use the array or list in the for loop.

Likes

translate

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
community guidelines
Community Beginner ,
Jun 05, 2020 Jun 05, 2020

Copy link to clipboard

Copied

Thank you both for the quick help!

Likes

translate

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
community guidelines