Field value edits within a loop

Community Beginner ,
Oct 27, 2018

Copy link to clipboard

Copied

I have a loop to iterate through all of the fields and copy them from a target pdf to the current pdf.

This currently works well with manually calling the fields, and with console.log within the loop. But as soon as the field.value is updated with the field.value of the other pdf the software crashes (infinite loop?). It even crashes if the field.value is manually set for all fields. I must be missing something simple..

Here is my code:

thisDoc = this;

var docName = app.browseForDoc();

app.openDoc(docName.cPath);

var d = app.activeDocs;

var thatDoc = d[d.length-1];

var i = 0;

while (i<thatDoc.numFields){

var oldName = thatDoc.getNthFieldName(i);

var old = thatDoc.getField(oldName);

if(old.type == "text"){

   if (thisDoc.getField(oldName) != null){

   var cur = thisDoc.getField(oldName)

cur.value = old.valueAsString;

   }

} i++;

}

thatDoc.closeDoc();

Any help would be appreciated.

Correct answer by shwarato | Community Beginner

Found the answer - the javascript api for adobe is single threaded, and as such a loop that relies on the commit chain (on a button/field object) will always fall outside of that commit chain if it's too long (I think!).

Creating this at a document level, after stuffing some lists, seemed to work perfectly well.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

208

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

Field value edits within a loop

Community Beginner ,
Oct 27, 2018

Copy link to clipboard

Copied

I have a loop to iterate through all of the fields and copy them from a target pdf to the current pdf.

This currently works well with manually calling the fields, and with console.log within the loop. But as soon as the field.value is updated with the field.value of the other pdf the software crashes (infinite loop?). It even crashes if the field.value is manually set for all fields. I must be missing something simple..

Here is my code:

thisDoc = this;

var docName = app.browseForDoc();

app.openDoc(docName.cPath);

var d = app.activeDocs;

var thatDoc = d[d.length-1];

var i = 0;

while (i<thatDoc.numFields){

var oldName = thatDoc.getNthFieldName(i);

var old = thatDoc.getField(oldName);

if(old.type == "text"){

   if (thisDoc.getField(oldName) != null){

   var cur = thisDoc.getField(oldName)

cur.value = old.valueAsString;

   }

} i++;

}

thatDoc.closeDoc();

Any help would be appreciated.

Correct answer by shwarato | Community Beginner

Found the answer - the javascript api for adobe is single threaded, and as such a loop that relies on the commit chain (on a button/field object) will always fall outside of that commit chain if it's too long (I think!).

Creating this at a document level, after stuffing some lists, seemed to work perfectly well.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

209

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
Oct 27, 2018 0
Adobe Community Professional ,
Oct 28, 2018

Copy link to clipboard

Copied

Try simplifying that first block of code to:

thisDoc = this;

var docName = app.browseForDoc();

var thatDoc = app.openDoc(docName.cPath);

var i = 0;

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...
Oct 28, 2018 0
Community Beginner ,
Oct 28, 2018

Copy link to clipboard

Copied

This is something I tried a little while ago, and it just doesn't access the other file 'thatDoc' after opening.

It's also not why this loop isn't working - as the code works great when printing instead of changing values.

I appreciate the idea though - and it's also something I can do within the loop - but I'm keeping it obtuse to try and see what does and doesn't work. 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...
Oct 28, 2018 0
Community Beginner ,
Nov 09, 2018

Copy link to clipboard

Copied

Found the answer - the javascript api for adobe is single threaded, and as such a loop that relies on the commit chain (on a button/field object) will always fall outside of that commit chain if it's too long (I think!).

Creating this at a document level, after stuffing some lists, seemed to work perfectly well.

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...
Nov 09, 2018 1