Copy link to clipboard
Copied
I'm working in Acrobat Pro XI on a 4 page document with a button that allows a template pages to be inserted after page 3. I created a button for this and it is working fine. But each page has a text field that is supposed to auto update all of the page numbers. The code looks like this:
event.value="Page "+(event.target.page+1)+" of "+this.numPages;
calculateNow();
On the four pages visible when the file is opened it is working perfectly; they will update from "Page 1 of 4" to "1 of 5", etc. without any problems. However, the inserted template pages are very random. Sometimes the first spawned one will work and say Page 4 of 5 but sometimes it will say Page 0 of 4. It's worth mentioning that even when it says Page 4 of 5 printing that page shows it saying Page 0 of 4 again. And any additional pages spawned after that will list a bunch of random numbers out of the correct number of pages. So it will look like "Page 7, 6, 5, 4, 31 of 9".
I've been looking through a lot of previously asked questions and haven't seen any quite like this.
If you want to add a new page for each press of the button, you can simplify the code to:
getTemplate("Addittional Collaborators").spawn(numPages, true, false);
but you'll have to add code to correctly set the "page x of y" field values. Post again if you have trouble with that part.
Copy link to clipboard
Copied
Where did you place that script exactly? Can you show the code you're using to spawn the template that includes the field that's used to display the "Page 1 of 4" text?
Copy link to clipboard
Copied
I placed the script in the custom calculation portion of a text field. And I have the spawn template button separate from the "Page x of y" text. The trigger is Mouse Up and the code is:
var a = this.getTemplate("Addittional Collaborators");
for (var i = 3; i<4; i++)
a.spawn(i, true, false);
Copy link to clipboard
Copied
I have a feeling your spawned field names are identical...
On Thu, Mar 10, 2016 at 11:11 PM, jfb45671162 <forums_noreply@adobe.com>
Copy link to clipboard
Copied
I just looked and you're right. Each field in the added pages are the same. So if the template spawns with unique field names that would theoretically solve the page numbering problem?
Copy link to clipboard
Copied
It's more complex than that. You already told the spawn object to rename the fields (by setting the second parameter as true).
I have a feeling the code you posted is not the real code you're using, because it doesn't make sense to use a loop just for one iteration (your loop will stop when i gets to 4, so it will only execute the code inside once). What's your real and full code? Also, on what page is the button that triggers the spawning located?
Copy link to clipboard
Copied
It would be helpful to know what the field names for the "page x of y" fields are after you spawn two or more additional pages. I think I would not use a calculation script for this and instead add the code that updates the fields after the code that spawns the template.
Copy link to clipboard
Copied
Each one is named "P3.Additional Collaborators.Page Number X". I'm not sure how to get them to spawn with unique names.
Copy link to clipboard
Copied
Are you pressing the button multiple times? That could explain it, as you're always spawning to the same page number... You need to spawn either to a new one each time by keeping a track of how many times the template was spawned before, or by spawning to a relative page number instead of an absolute one (a relative number could be the last page number in the file, for example).
Copy link to clipboard
Copied
I've been hitting the button multiple times. So the for (var i = 3; i<4; i++) should be set to a relative position?
Copy link to clipboard
Copied
Yes, like George's example above.
Copy link to clipboard
Copied
If you want to add a new page for each press of the button, you can simplify the code to:
getTemplate("Addittional Collaborators").spawn(numPages, true, false);
but you'll have to add code to correctly set the "page x of y" field values. Post again if you have trouble with that part.
Copy link to clipboard
Copied
Thank you so much. I felt like I was hitting my head on the wall trying to figure it out.
Copy link to clipboard
Copied
Hi all
I have a further issue where my text is not updating. I have a spawned page button on page 2 of a 3 page doc that inserts a copy of p2 between p2 and p3. The code I use is successful in doing this
"var pagecount = 0;
var pagepos = 0;
pagecount = this.numpages;
pagepos = pagecount-1;
gettemplate("newtemplate").spawn({npage:pagepos, brename: true, boverlay:false});"
Howver the footer does not change the numbering. I also tried adding a text box with the following code in the custom calculation but get nada
this.value="page"+this.pagenum +" of "+this.numpages;
calculatenow();
Any ideas?
Thanks in advance
Copy link to clipboard
Copied
Is that really the code you're using? I ask because there are a few mistakes that would cause it not to execute.
Copy link to clipboard
Copied
Hi George. Yep that's the code. First part for spawning is good. Second part I reallllly need help on.
Copy link to clipboard
Copied
Hi George. Any chance you could point me in the right direction?
Thanks in advance
damian
Copy link to clipboard
Copied
Looks like George is busy, so I'll step in.
There are a number of things that are wrong with you scripts. Have you ever checked the JavaScript console for errors? When you do any development with Acrobat's JavaScript, you have to keep an eye on the console.
You need to be very familiar with the Acrobat JavaScript API documentation, whenever you find an error reported on the console, you will have to figure out what the correct syntax is. In your case, let's look at the call to get the template. The correct syntax is documented here: Acrobat DC SDK Documentation - Doc.getTemplate()
You'll notice that the function name is spelled with a capital "T" - JavaScript is case sensitive, so it makes a difference if you use gettemplate vs. getTemplate. The next problem is not a big one, because in your case you got lucky: the getTemplate() method is part of an object, and in general you should specify which object it belongs to. In this case, we are dealing with a button handler, to the active object ("this") is your currently open document, so you would use "this.getTempate()". This will bite you when you don't specify the object once you are a function call or two deeper in your all hierarchy.
Again, JavaScript is case sensitive, and that not only applies to function or method names, but also to properties. You are using "npage", "brename" and "boverlay" in the object that you pass in as a parameter to getTemplate(). These are actually spelled "nPage", "bRename" and "bOverlay" (see here: Acrobat DC SDK Documentation - Template.spawn()​)
Also, Doc.numPages and Doc.pageNum also have capitals in them.
Now, just to make things a bit more efficient, the following should work:
this.getTemplate("newtemplate").spawn({ nPage:this.numPages, bRename:true, bOverlay:false });
this.calculateNow();
You see that I am calling Doc.calculateNow() from the same script that spawns the page. The problem with your solution is that calculateNow() was called from inside the calculation script - when the calculation was actually running.
So, let's take a look at the field's calculation script. You have again problems with capitalization, but there are two other problems here. First, to set the field value to the result of a collocation script, you use "event.value" (at least with AcroForms, in XFA or LiveCycle Designer forms you would use the "this" object). And, if you want to display "page x of y", you have to adjust the page number that you get by 1 - the first page in a PDF document is page 0. To get the page that a form field is on, you would use "event.target.value" - at least if there is only one copy of your form field in the document, otherwise it gets a bit more complex. This makes your final calculation script to look like this:
event.value= "page "+ (event.target.page + 1) +" of " + this.numPages;
Copy link to clipboard
Copied
Thanks Karl. I have only just had time to breathe and get back t it. This has definitely resolved my problem. Again, much appreciated.
Damian
Copy link to clipboard
Copied
I've read the calculateNow() in the 2021 SDK, but I've never implemented it.
Questions about calculateNow():
I'm having an issue with spawning pages, so I put the spawn in a calculate script. I also put 'this.calculate = false;' at the bottom of the calculate script.
I then put 'this.calculateNow();' on a MouseUp event of a checkbox group. The calculation is checking the values of the checkbox as well as the number of pages in the document and will either delete pages from nStart 1 to numPages-1 then spawn or do nothing--at least in theory. I'm trying this method because the spawn works and the MouseUp events work, but not when all put in the same button. that crashes Acrobat Pro and Reader.
Copy link to clipboard
Copied
No, you don't need to do that. Setting the calculate method to false stops all calculations in the entire file, for good, until you set it back to true, or call calculateNow. You don't need to do that. Just call it once and that's it.
Copy link to clipboard
Copied
Thanks. The way I read your answer is if I call calculateNow, I'll then need to set calculations to false again. Is that correct?
I want the calculation to happen only when the checkbox group is clicked. I was considering calculateNow() at the top of the script, then resetting it with calculate = false at the bottom OR adding calculate = false to the bottom of the calculation script.
Copy link to clipboard
Copied
I never said anything like it. If you want to stop all calculations from happening then you can set calculate to false, yes.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more