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

spawn template off dropdown list

Community Beginner ,
Apr 08, 2024 Apr 08, 2024

Copy link to clipboard

Copied

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. 

TOPICS
Edit and convert PDFs , JavaScript , PDF , PDF forms

Views

357

Translate

Translate

Report

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 09, 2024 Apr 09, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 09, 2024 Apr 09, 2024

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

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 09, 2024 Apr 09, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

Hello Thom,

 

Thank you for your help, but I cannot seem to be able to set this up the correct way,  I am attaching a copy of the document i am working on. Could you please take a look? Much appreciated!

Votes

Translate

Translate

Report

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 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

The templates in your file are not called "human", but "Research1" to "Research3"... Also, do not use the Calculate event for this as it will cause new pages to be spawned all the time. Use the Validate event, as Thom pointed out.

Votes

Translate

Translate

Report

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 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

Hello and thank you Try67.

I was not sure where to add the code. I have changed the template name to "research1", "research2" and "research3" since the template are 3 pages that go together. Added the code under the custom validatation. Not sure what else could be wrong. Thank you in advance for your assistance.

Votes

Translate

Translate

Report

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 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

 

So, you did not properly explain that there were 3 separate templates. That changes things. Also The script I provided was specifcally written for a KeyStroke Script. It will not work in a validation script.

My first script will work. But it has to be changed to accomodate the 3 different templates. 

Here's a working script, but now you have a couple of other problems. First, How does the user select the "1" option. 

Second, what happens when they go back and select a different number? Does it add to the existing or just change the number of added sections?

 

if(!isNaN(event.value))
{
    var nLen = Number(event.value);
    var oTmpl1 = this.getTemplate("Research1");
    var oTmpl2 = this.getTemplate("Research2");
    var oTmpl3 = this.getTemplate("Research3");
    var oXObj1 = null, oXObj2 = null, oXObj3 = null;
    var nTgtPg;
    for(i=0;i<nLen;i++)
    {
      nTgtPg = this.numPages;
      if(oXObj1)
      {
          oTmpl1.spawn(nTgtPg, false, false, oXObj1);
          oTmpl2.spawn(nTgtPg+1, false, false, oXObj2);
          oTmpl3.spawn(nTgtPg+2, false, false, oXObj3);
      }
      else
      {
          oXObj1 = oTmpl1.spawn(nTgtPg, false, false);
          oXObj2 = oTmpl2.spawn(nTgtPg+1, false, false);
          oXObj3 = oTmpl3.spawn(nTgtPg+2, false, false);
      }
    }
}

 

 

 

 

 

 

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

Votes

Translate

Translate

Report

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 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

Thank you for the response Thom. Not sure what would be the easier way to trigger the spawn of templates. For what i hope would work is that the user will select from the dropdown the 1, 2 or X number of templates and they will get generated. Not sure if this is feasible.

Not sure how to address the second issue, would adding the templates one at the time be easier? Like an action button to spawn one set of templates (1,2 and 3) at the time? And by pressing it again generate another one? Then probably add a delete button to each to delete at will. Thank you again for all your guidance! Appreciated it!

 

Votes

Translate

Translate

Report

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 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

I guess when someone changes the number, it will modify/add/remove to adjust to the number of templates (if feasible)

Votes

Translate

Translate

Report

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 10, 2024 Apr 10, 2024

Copy link to clipboard

Copied

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

 

 

 

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

Votes

Translate

Translate

Report

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 11, 2024 Apr 11, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 11, 2024 Apr 11, 2024

Copy link to clipboard

Copied

The code works wonderfully! Is there a way to include the table (page 2) and have the templates spawn before this page, so that would make this the last page/conclusion of the document.  Thank you again for all your help!

Votes

Translate

Translate

Report

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 11, 2024 Apr 11, 2024

Copy link to clipboard

Copied

LATEST

Yes, it's just a matter of adjusting the "nTgtPg" variable to target the desired location. You'll also need to adjust the end page parameter in the deletePages function so only the template pages are deleted. 

 

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

Votes

Translate

Translate

Report

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