Skip to main content
Participant
March 15, 2021
Answered

Multiple Templates and Spawning Java Script

  • March 15, 2021
  • 3 replies
  • 4552 views

I have a two page document that I would like the end user to be able to click a button and two new pages spawn.

 

I was able to figure out how to get one page to spawn but I can't seem to get both from the same button click. 

This is the java script I am using: 

var a = this.getTemplate ("Account Placement New"); a.spawn();

 

The new templates is Guarantor Placement New.

This topic has been closed for replies.
Correct answer Nesa Nurani

Use this code and replace xxx with template 1 name  and yyy with template 2 name :

this.spawnPageFromTemplate("xxx",this.numPages, false, false);
this.spawnPageFromTemplate("yyy",this.numPages, false, false);

3 replies

Layne5C26Author
Participant
December 7, 2023

I am utalizing this java script to generate new page templates : 

var a = this.getTemplate ("Guarantor Placement New"); a.spawn();var a = this.getTemplate ("Account Placement New"); a.spawn();

 

The original page has a calculated field where we are using sum (+) to calculate certain fields.  The problem that creates is each time a new template is spawned it will change the name of the fields to have P3.Account Placement New. (field name from origianl page) . Is there a custom calculated script with a wildcard that will pick up those fields? 

 

 

Thom Parker
Community Expert
Community Expert
December 7, 2023

The calculation script has to build the field names on the fly, by adding the spawned template prefix to the base field name. 

The spawed prefix  can be extracted from the name of the calculated field, like this:

 

var cPrefix = (event.target.page != -1)?event.targetName.split(".").slice(0,2).join(".")+".":""; 

 

Add this code to the top of your calculation script.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
April 21, 2021

I had this same question, I was wondering if I have to add +1 for the second template (in bold below):

 

getTemplate("xxx").spawn(this.numPages, false, false);
getTemplate("yyy").spawn(this.numPages +1, false, false);

Thom Parker
Community Expert
Community Expert
April 22, 2021

The first spawn will increase the number of pages by one, so adding one to the number of pages is unecessary. But it also shouldn't matter because the last page is the last page. Try it and see what happens. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
September 22, 2023

This works, great, if I want my spawned pages to appear at the end of my form, however, I need the pages to be added after the page with my checkbox (page 2) that creates the spawning.

 

What is the code to ensure all 6 pf my spawned pages appear after page 2?

 

I have one that works really well, but am having trouble getting it to work for the spawning of multiple pages:

 

var a = this.getTemplate ("Risks");
a.spawn({nPage: this.pageNum+1, bRename: true, bOverlay: false});
this.getField("P" + (this.pageNum+1 - 1) + "Risks").setFocus();

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
March 15, 2021

Use this code and replace xxx with template 1 name  and yyy with template 2 name :

this.spawnPageFromTemplate("xxx",this.numPages, false, false);
this.spawnPageFromTemplate("yyy",this.numPages, false, false);

Layne5C26Author
Participant
March 15, 2021

Thank you that worked perfectly!