Skip to main content
Inspiring
October 6, 2023
Answered

Button to show/ hide page templates

  • October 6, 2023
  • 2 replies
  • 8666 views

I have a check box button on page 1.  The following code works to show/hide page 2 .  Click the button, show page 2.  Unclick the button, hide page 2.

 

However, it gives me a "bad parameter" despite working.  Any ideas?

 

if(event.target.value!="Off")
{this.getTemplate("2" ).hidden=false;}
else
{this.getTemplate("2" ).hidden=true;}

This topic has been closed for replies.
Correct answer Thom Parker

It has two pages.  First page is always visible.  2nd page is what the button will show/hide.  Button is on page 1.

How do I set target page number?

 


Ok the target page isn't set. That is the source of the error.   Page numbers in the Acrobat JavaScript model are zero based. Page #1 is 0, and Page #2 is 1;

 

Use this code:

if(event.target.value!="Off")
{this.getTemplate("2" ).spawn(1, false, false);}
else
{this.deletePages(1);}

 

 

2 replies

Thom Parker
Adobe Expert
October 6, 2023

Is the Template named "2"?  If not I don't see how the code could work. Also, unhiding a template always places the page at the end of the document. You can't specify the page where it will appear. 

 

Also, hiding and showing a template will not work in Reader. The correct way to use a template is to "spawn" it. 

This thread provides a checkbox spawn solution:

https://community.adobe.com/t5/acrobat-discussions/spawn-templates-after-a-specific-page-with-checkboxes/m-p/14137819#M432760

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
MBChelsAuthor
Inspiring
October 6, 2023

I tried the code from the thread you provided and nothing happened

if(event.target.value!="Off")
{this.getTemplate("2" ).spawn(nTargetPageNumber, false, false);}
else
{this.deletePages(nTargetPageNumber);}

 

MBChelsAuthor
Inspiring
October 11, 2023

If page #1 is never spawned or deleted then there is no reason for it to be a page template.  In fact, making it a template is problematic. Remove it from the page template so it is a normal page. 

 

Since page #2 is spawned and deleted, it does need to be setup a as a page template. But it needs to be set to hidden. The checkbox and script will need to be synchronized with the template, so there is no confusion. The simplest way to do this is to check the number of pages in the document. If there are two pages, then obviously the template has already been spawned, and if there is only one page then the template page has already been deleted. Here's a modification of the script.

if((event.target.value!="Off") && (this.numPages == 1))
   this.getTemplate("2" ).spawn(1, false, false);
else if(this.numPages > 1)
   this.deletePages(1);

 

The error message that pops up is likely caused by some type of inernal structural problem on the page. Bernd is correct. You need to recreate this page. In fact, I'd suggest starting over and recreate the entire document. 

 


Ok.  I will recreate the entire document and let you know.

MBChelsAuthor
Inspiring
October 6, 2023

Also, this bad parameter error prevents me from saving this document.