So I implemented try67's advice and this is the end product. I am just going to leave it here if anyone else bumps into the same issue and maybe this will save them some time (and frustration 🙂 ).
I also looked up how to save a variable for later (if the PDF is closed and reopened). This is how you can do it, it basically just saved the variable into the metedata. So enter this into the console to set the variable:
this.info.var_counter = 0;
This is the code. I put it as an action of the button (named Button_1), and set the page template.
1. At the click of the button, all of the previously spawned pages are going to move to the end of the document.
2. A new page is going to spawn with unique field names
3. All of the pages are going to get moved back (to the next page after the page with the button)
4. The amount of total spawned pages get saved as a variable into the metadata
// the button that you click
button_name = "Button_1"
// name of page template
template_name = "template_name"
// tracking amount of pages that have been spawned already
var counter = this.info.var_counter
// we move all the pages to the end of the document
for (i = 0; i < counter; i++) {
// moving the specific page (the one after the button)
this.movePage(this.getField(button_name).page + 1);
}
// spawning a new page from template
this.getTemplate(template_name).spawn({nPage: this.numPages, bRename: true, bOverlay: false});
// we increase the spawned page counter
counter++
// moving all the pages back to where they should be
for (i = 0; i < counter; i++) {
// moving the page after the button
this.movePage(this.numPages - 1, this.getField(button_name).page)
}
// we save the amount of spawned pages for future use
this.info.var_counter = counter;
Hope this helps!