Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Multiple Templates and Spawning Java Script

Community Beginner ,
Mar 15, 2021 Mar 15, 2021

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.

TOPICS
Create PDFs , JavaScript , PDF forms
4.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 15, 2021 Mar 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);

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 15, 2021 Mar 15, 2021

Thank you that worked perfectly! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 15, 2021

You should not be using this method. It's deprecated and could stop working at any moment when a new update is released. Use the spawn method of the Template object, instead.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 15, 2021

I agree with Try67, use the "template.spawn" method. Do not use the older and deprecated "doc.spawnPageFromTemplate"

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 15, 2021 Mar 15, 2021

I am incredily new to this so thank you both for the input. I'm not entirely familar with how to use the "template.spawn" method as opposed to the "doc.spawnPageFromTemplate" that Nesa provided. 

 

Would this new method look like :

template.spawn("xxx",this.numPages, false, false);
template.spawn("yyy",this.numPages, false, false);

 

This is a previous formatting I was using but it would only pull one template at a time : 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2021 Mar 15, 2021

Try this:

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 21, 2021 Apr 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);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2021 Apr 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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 21, 2023 Sep 21, 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2023 Sep 22, 2023

That's not a good idea if you want to spawn the page more than once, as you'll get duplicate field names.

If you do want to do it just change nPage: this.pageNum+1 to nPage: 1.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 07, 2023 Dec 07, 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? 

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 07, 2023 Dec 07, 2023
LATEST

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines