Skip to main content
jctremblay
Community Expert
Community Expert
October 7, 2019
Question

create a loop for creating fields for a range of pages

  • October 7, 2019
  • 1 reply
  • 1358 views

I’m able to create fields on everypages of a PDF using this:

for (var nPage = 0; nPage < this.numPages ; nPage++) {
var f = this.addField("myField", "text", nPage, [200, 600, 300, 660]);
}

or to a specific pages: 

//Page 2
var f = this.addField("myField", "text", 1, [200, 600, 300, 660]);

or all pages except the first or the last.

But now I need to add fields to a specific set of page ex: 2-4, 6. It will probably involve an array with a loop. But I’m lost.
Any clue on how to create an array of pages numbers, and loop the process of creating fields on them? Or any tutorials (simple and clear) on how to create/use arrays and loops would be welcomed.

Thanks!

This topic has been closed for replies.

1 reply

Bernd Alheit
Community Expert
Community Expert
October 7, 2019

Try this:

for (var nPage in [1, 2, 3, 5]) {
  var f = this.addField("myField", "text", nPage, [200, 600, 300, 660]);
}
jctremblay
Community Expert
Community Expert
October 7, 2019
This create a field on the first 4 pages of the doc. Instead of page 2, 3, 4, and 6
Bernd Alheit
Community Expert
Community Expert
October 7, 2019
Then you must use something other.