With the help of prevously asked questions/answers, various tutorials and lots of luck, I've managed to create a button that spawns a new page. I've also managed to add page numbers in the format 1 of n to a specific field on the pages.
The problem I'm having is if I delete one of the spawned pages, how do I get the JS to recalculate the page numbers so it goes from, for example 1 of 3, 2 of 3, 3 of 3 to 1 of 2, 2 of 2 (the document will always start with 2 pages)
In the custom calculation of my page number field, this is the code I used:
event.value = (event.target.page+1) + " of " + this.numPages;