Copy link to clipboard
Copied
I have created a button to add a page using a template, but it is adding the page at the end.
I need the page to be added right away behind the previous page and need the page numbering in the footer to be right, which is not when the new page is added.
I am not good with javascript. Can someone help me with the coding?
Copy link to clipboard
Copied
The first argument of the spawn() function is the page number, so you have control over where it is placed. Its just a mater of knowing the correct page number. So, please post the code you are using right now.
And provide some more information.
1. On which page is the button that spawns the new page?
2. How do you know where the new page should be inserted?
Copy link to clipboard
Copied
My button is on Page 5, so it needs to added as Page 6 and be numbered Page 6. If an additional one is added that one becomes Page 7 and numbered Page 7. So far and so far.
var contPage=this.getTemplate("Additional Testing Items"); contPage.spawn({nPage:this.numPages, bRename:true, bOverlay:false});
Thanks
Copy link to clipboard
Copied
Ok, so your current code is adding it as the last page.
This variation adds the tempate after the page that contains the button
contPage.spawn({nPage:event.target.page +1, bRename:true, bOverlay:false});
However, it sounds like you want these pages stacked up one after the other, to do this you need a way to keep track of where the last spawned template is placed. The most robust way to do this is to search for a field that you know is on the template and has a unique name, and just keep track of the page numbers.
var nLastPage = event.target.page;
for(var i=0;i<this.numFields;i++)
{
strFldName = this.getNthFieldName(i);
if(/MyUniqueFieldName/.test(strFldName))
{
oFld = this.getField(strFldName);
if(oFld.page > nLastPage)
nLastPage = oFld.page;
}
}
contPage.spawn({nPage:nLastPage+1, ....);
Copy link to clipboard
Copied
I tried using the code that you posted isn't working for me right when i input everything in the code it tells me that i have an illegal characteristic in line 5.
var nLastPage = event.target.page;
for(var i=0;i<this.numFields;i++)
{
strFldName = this.getNthFieldName(“Firearm+”);
if(/MyUniqueFieldName/.test(strFldName))
{
oFld = this.getField(strFldName);
if(oFld.page > nLastPage)
nLastPage = oFld.page;
}
}
Copy link to clipboard
Copied
You must only use straight quotes, either single or double.
Copy link to clipboard
Copied
Am I putting quotation in every part or just where I have them in the Firearm+
Copy link to clipboard
Copied
Maybe explain what you're trying to achieve, as currently your code doesn't make much sense...
Copy link to clipboard
Copied
I trying to use this code on a page template button to make sure that when adding the pages it just keep sacking on top of each other after one specific page because I have two page template button I'm trying to use this java script for
Copy link to clipboard
Copied
You should not be specifying the field name, then. Use the template code Thom provided.
Copy link to clipboard
Copied
Ok, so first, this line of code
strFldName = this.getNthFieldName(i);
The input to the function must me a number. Leave it as "i", as it was in the original code I posted.
Second, the text you need to change in order to match the field you're looking for is in this line
if(/MyUniqueFieldName/.test(strFldName))
I put "MyUniqueFieldName" in this line as a placeholder for your unique field name.
Do not include the "+" or any other punctuation character when you replace this name. In fact, if your field name contains a "+", remove it immediately. It is very bad form and potentially harmful to use special characters in field names.
Copy link to clipboard
Copied
Okay I'm try it this morning.
Copy link to clipboard
Copied
var nLastPage = event.target.page;
for(var i=0;i<this.numFields;i++)
{
strFldName = this.getNthFieldName(i);
if(/Firearm12/.test(strFldName))
{
oFld = this.getField(strFldName);
if(oFld.page > nLastPage)
nLastPage = oFld.page;
}
}
contPage.spawn({nPage:nLastPage+1, ....);
Here is the new one I did today, also when i copy and paste everything into the PDF it makes everything space out more then it is.
Copy link to clipboard
Copied
The reason extra lines are added to the code when it is pasted into Acrobat is because the editing tool used to enter the code is inserting complex line endings. This is quite common and a standard practice for certain types of text files and operating systems. However, any editor that does this might also be inserting odd invisible characters into the text, so you should consider using a different editing tool, such as the plain NotePad or the much better NotePad++.
What is the error in this code and on what line?
I notice that the template spawn function is not complete. Is this on purpose? And is the "contPage" template object defined somewhere in this script, but not shown?
Copy link to clipboard
Copied
so when I used this code: var contPage=this.getTemplate("Additional Testing Items"); contPage.spawn({nPage:event.target.page+1, bRename:true, bOverlay:false});
It effectively gives me the page right after, but I realized that if I wrote something in the added page, once I clicked to add another page it copies what was wrote in the previously added page. How to solve this?
Copy link to clipboard
Copied
It says (SyntaxError:invalid Property ID 12: at line 13)
Copy link to clipboard
Copied
Ok, is this line 13?
contPage.spawn({nPage:nLastPage+1, ....);
If so, then as I indicated in my last post, it is not complete. There are 2 issues.
1. Where is the "contPage" object declared/defined?
2. "..." is not a valid entry for this function. This is the cause of the error message. You need to use the code you used in the first place.
I used this syntax as shorthand, cause I'm not going to type the whole thing. But you need to use the entire line in the code.
contPage.spawn({nPage:nLastPage +1, bRename:true, bOverlay:false});
Copy link to clipboard
Copied
Yes that's line 13
Copy link to clipboard
Copied
Okay here's the new code i made
var nLastPage = event.target.page;
for(var i=0;i<this.numFields;i++)
{
strFldName = this.getNthFieldName(i);
if(/Firearm12/.test(strFldName))
{
oFld= this.getField(strFldName);
if(oFld.page > nLastPage)
nLastPage = oFld.Page;
}
}
contPage.spawn({nPage:nLastPages +1, bRename:true, bOverlay:false});
Copy link to clipboard
Copied
It all looks good !! Does it work?
Copy link to clipboard
Copied
It not pulling the page template I have in place.
Copy link to clipboard
Copied
when I press the button
Copy link to clipboard
Copied
But there is no error reported in the console window?
Again, where is the "contPage" variable defined in your script?
Copy link to clipboard
Copied
var contPage=this.getTemplate("Additional Testing Items"); contPage.spawn({nPage:this.numPages, bRename:true, bOverlay:false});
Do I need to grab the first part of this line to get the template.
Copy link to clipboard
Copied
I want it defined as the first part of this code.