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

Button to show/ hide page templates

Participant ,
Oct 06, 2023 Oct 06, 2023

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

TOPICS
Edit and convert PDFs , JavaScript , Modern Acrobat , PDF , PDF forms
5.7K
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 ,
Oct 06, 2023 Oct 06, 2023

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

 

 

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

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
Participant ,
Oct 06, 2023 Oct 06, 2023

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

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 ,
Oct 06, 2023 Oct 06, 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-checkb...

 

 

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
Participant ,
Oct 06, 2023 Oct 06, 2023

Yes sir.  Page 1 template is "1" and page 2 template is "2"

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
Participant ,
Oct 06, 2023 Oct 06, 2023

Also, there are only two pages in this document.  I mean it works but I keep getting a bad parameter error and it messes up the document where I can't save it and it starts acting all weird.

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
Participant ,
Oct 06, 2023 Oct 06, 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);}

 

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 ,
Oct 06, 2023 Oct 06, 2023

A PDF cannot have 0 pages. Both pages cannot be hidden templates

 

About your script. Where is "nTargetPageNumber" set? 

Did you check the console window for errors?

 

 

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
Participant ,
Oct 06, 2023 Oct 06, 2023

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?

 

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 ,
Oct 06, 2023 Oct 06, 2023

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

 

 

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
Participant ,
Oct 10, 2023 Oct 10, 2023

Hi Sir,

 

When I click the checkbox, page 2 is deleted.  When I unclick it, nothing happens.  There is no way to bring back page 2.  So this code doesn't appear to work still.

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
Participant ,
Oct 10, 2023 Oct 10, 2023

Something I just noticed,

 

My original code works so long as page 1 is the only page in the window.  If I scroll down midway to where the bottom of page 1 and the top of page 2 are in the window, then I get the bad parameter error.  Actually, if any part of page 2 is visible, then I get the bad parameter error.  The code still works regardless.  But what is weird, is that nothing comes up in the debugger.  

 

Any ideas?

 

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

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 ,
Oct 10, 2023 Oct 10, 2023

The bad parameter is at page 2.

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
Participant ,
Oct 10, 2023 Oct 10, 2023

Why is there a bad parameter when the code works and there are no errors?

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 ,
Oct 10, 2023 Oct 10, 2023

There is no connection between the error and the script.

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
Participant ,
Oct 10, 2023 Oct 10, 2023

So what do I do?

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 ,
Oct 10, 2023 Oct 10, 2023

Re-create page 2.

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 ,
Oct 10, 2023 Oct 10, 2023

The spawn code is correct, so the error is somewhere else. Forget about the original show/hide code, it can't be used for a distributed document. 

 

You state that when you click the checkbox, page 2 is deleted. So your saying the template page is visible before you click the checkbox? If so, then you are deleteing the template. So of course nothing happens when you uncheck, because the template doesn't exist at that point. 

Template pages need to be hidden before they are put into operation. 

 

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
Participant ,
Oct 10, 2023 Oct 10, 2023

Yes sir.  Page 1 and 2 templates are visible when the checkbox is clicked.  When I uncheck the checkbox, page 2 is deleted.

 

So do I need to have two copies of page 2 template and hide one of them so then it will continually spawn off the hidden copy?  Or something else?

 

By the way, I also get the bad parameter error as well.

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
Participant ,
Oct 10, 2023 Oct 10, 2023

I figured it out.  It is working now.  The "sequence" was off I think.   I removed (cut) the code, unchecked the box, pasted it back in, hid page 2 template, then checked the box and it works.

 

But still I get a bad parameter error if I scroll down midway to where the bottom of page 1 and the top of page 2 are in the window or if any part of page 2 is visible.  I still get the bad parameter error.  Still nothing comes up in the debugger.  

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
Participant ,
Oct 10, 2023 Oct 10, 2023

Perhaps a workaround would be to jump up to page 1 before deleting the 2nd page (since everything works without error so long as none of page 2 is in the window?  I don't know how to do that though.

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 ,
Oct 10, 2023 Oct 10, 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. 

 

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
Participant ,
Oct 11, 2023 Oct 11, 2023

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

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
Participant ,
Oct 12, 2023 Oct 12, 2023

I recreated the entire document and it still gave me bad parameter error....very strange

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 ,
Oct 13, 2023 Oct 13, 2023

What is your process for creating the document?

Can you post the PDF?

 

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
Participant ,
Oct 13, 2023 Oct 13, 2023

Yes sir.  See attached.

 

I found a workaround though.  I added a custom zoom level action at the beginning before running all the java actions.  For whatever reason, the bad parameter error never occurs so long as page 1 is the only page visible in the window.  If any part of page 2 is visible, the error displays (even though the code runs successfully).  This way anytime I check or uncheck the box, the first thing it does is jump to page 1 before hiding/ unhiding page 2 (in addition to the other actions).

 

As for the process, I just started from the top of page 1 and worked my way down the page onto page 2 adding all of the fields, etc.

 

 

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