Skip to main content
Participating Frequently
April 9, 2024
Question

spawn template off dropdown list

  • April 9, 2024
  • 2 replies
  • 2753 views

How can i generate a 3 page template depending on a dropdown choice?

For example on the dropdown, choices 1 - 10 numbers

if the # 5 is chosen, then to spawn 5 set of templates.

 

Also need these to spawn in the middle of the document, so leaving a 1 page conclusion always at the end. 

This topic has been closed for replies.

2 replies

Participating Frequently
April 10, 2024

Thank you for the response Thom. I have changed this to my template name 

var oTmpl = this.getTemplate(....);

 

However, when i choose any number on the dropdown, nothing happens. Is there something else that needs to be set here? Sorry for the basic questions, im pretty new to this. 

Thom Parker
Community Expert
April 10, 2024

Ahh, my bad. The value of a list field is the value of the export value of the selected item. However, in the Validate event, event.value returns the display text, not the export. 

So there are two ways to fix this.

1) Search the list items for the matching display value to get the index, which can then be used to get the export value. 

2) Move the code to the Keystroke event, which provides the export value in the event object. 

 

Here's the code for the keystroke:

if(!event.willCommit && !isNaN(event.changeEx))
{
    var nLen = Number(event.changeEx);
    var oTmpl = this.getTemplate(....);
    var oXObj = null;
    var nTgtPg;
    for(i=0;i<nLen;i++)
    {
      nTgtPg = this.numPages-2;
      if(oXObj)
          oTmpl.spawn(nTgtPg, false, false, oXObj);
      else
          oXObj = oTmpl.spawn(nTgtPg, false, false);
    }
}

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
April 11, 2024

The script I provided above is a Validation Script, and it works with your document. 

So, there are two ways to adjust the number of spawned pages based on selected number. 

1) Manage the template spawning. In this scenario template pages are spawned or deleted based in the difference between the current selection and the previous selection. 

2) Delete all template pages and start over. 

 

There are some that would say #1 is the more efficient, elegant, and purer solution. #2 is more of a brute force solution, but it works the same and is much easier to implement. 

 

Add this code to the top of the Validation Script I provided above

 

if(this.numPages > 2)
   this.deletePages(2,this.numPages-1);

 

 

 


Thank you Thom, this works amazingly! Very elegant! 100/100!

Thom Parker
Community Expert
April 9, 2024

Add this code to the validate script for the dropdown

 

if(!isNaN(event.value))
{
    var nLen = Number(event.value);
    var oTmpl = this.getTemplate(....);
    var oXObj = null;
    var nTgtPg;
    for(i=0;i<nLen;i++)
    {
      nTgtPg = this.numPages-2;
      if(oXObj)
          oTmpl.spawn(nTgtPg, false, false, oXObj);
      else
          oXObj = oTmpl.spawn(nTgtPg, false, false);
    }
}
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often