Skip to main content
Participant
March 20, 2018
Question

Page numbering when button adds template page

  • March 20, 2018
  • 2 replies
  • 2203 views

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?

This topic has been closed for replies.

2 replies

Participating Frequently
March 27, 2018

It says (SyntaxError:invalid Property ID 12: at line 13)

Thom Parker
Community Expert
Community Expert
March 27, 2018

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});

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
March 27, 2018

Yes that's line 13

Thom Parker
Community Expert
Community Expert
March 20, 2018

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?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
March 20, 2018

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

Thom Parker
Community Expert
Community Expert
March 20, 2018

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, ....);

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often