Skip to main content
Inspiring
February 28, 2024
Answered

Deleting Spawned Pages and final page buttons issue

  • February 28, 2024
  • 4 replies
  • 2744 views

Hope someone can help me out. I have a 3 page form (pages 2 and 3 are hidden as template pages).

 

On page 1 there are two Yes/No radio buttons - Q1 will spawn p2 if yes is selected and Q2 will spawn 3 if yes is selected. The problem I have not been able to solve is deleting the spawned pages when No is selected.

They can be clicked in any order so Q2 could be answered before Q1, I've set the spawn code so that pages appear in the correct order, no matter which Q is answered first. Both pages only spawn once so the code for them is set as follows (1 changed to 2 for second spawn page):

this.spawnPagesFromTemplate({
c.template: "Add 1",

nPage: 1,

bOverlay:false,
bRename:false,
)}


There is a hidden field called Spawn 1 and Spawn 2 on the two spawned pages so I thought I could use the code I found elsewhere in the forum to delete the spawned pages, but it's not working. I suspect the issue lies with my initial spawn code.

var f = this.getField("Spawn 1").page;
this.deletePages(f);

The second part of my problem is I have a submit button and caveat copy that should appear on the last page of the document (which could be any one of the 3 pages depending on Q1/2 answers). I initially tried to control this via the Q1 and Q2 Yes/No buttons but it only works if the answers are not changed. I thought about setting the visibility as conditional on page count but can't figure out where I would put any code for that - could I add the code to the Q1 and 2 yes/no buttons or would that clash with spawn commands?

Also I'm a bit unsure how to build/combine the code for all 3 pages, I made a start but know this isn't right and doesn't hide the other page footers:
if(this.numPages==1) 

{

this.getField("P1 Footer").display = display.visible; this.getField("P1 Submit").display=display.visible}
else

if(this.numPages==2)
{this.getField("P2 Footer").display=display.visible; this.getField("P2 Submit").display=display.visible}

If anyone can point me in the right direction it would be much appreciated!

This topic has been closed for replies.
Correct answer Ozkim24

Thought I'd put the solution in one place for ease of anyone else looking. Thanks to those who helped. Hidden calculation field is switched round which got rid of that Bad parameter warning.

There are two Yes/No radio button questions at the top of page 1 of the form. I needed Q1 to spawn p2 if yes (and delete if No) and Q2 to spawn p3 if yes (and delete if No).


Q1: Yes:                                                                                                    Q1: No:
var a_this.getTemplate("Spawn 1");                                                     var f=this.getField("Spawn 1.1").page;

a.spawn({nPage:1,bRename:false,bOverlay:false})                            this.deletePages(f);

Q2: Yes:                                                                                                    Q2: No:
getTemplate("Spawn2").spawn(this.numPages,false,false);             var f=this.getField("Spawn 2.1").page;

                                                                                                                 this.deletePages(f);

 

I attached show/hide commands to the Yes/No radio buttons for these questions. To show hide the submit/button - intention is to only have it visible on last page of document which can be page 1, 2 or 3.

Q1 Yes shows P1 footer/submit button, Q2No, hides them. Q2 Yes hides the p1 and 2  items, Q2 No shows the p2 items. This could still end up with two footer/submit buttons visible if the answers to the first two questions were changed. Adding this hidden calculation field fixed that.

 

if(this.numPages>2){

this.getField("P1.Footer").display=display.hidden;

this.getField("P1.Submit").display=display.hidden;

}

else if(this.numPages<2){

this.getField("P1.Footer").display=display.visible;

this.getField("P1.Submit").display=display.visible;

}

4 replies

Ozkim24AuthorCorrect answer
Inspiring
March 12, 2024

Thought I'd put the solution in one place for ease of anyone else looking. Thanks to those who helped. Hidden calculation field is switched round which got rid of that Bad parameter warning.

There are two Yes/No radio button questions at the top of page 1 of the form. I needed Q1 to spawn p2 if yes (and delete if No) and Q2 to spawn p3 if yes (and delete if No).


Q1: Yes:                                                                                                    Q1: No:
var a_this.getTemplate("Spawn 1");                                                     var f=this.getField("Spawn 1.1").page;

a.spawn({nPage:1,bRename:false,bOverlay:false})                            this.deletePages(f);

Q2: Yes:                                                                                                    Q2: No:
getTemplate("Spawn2").spawn(this.numPages,false,false);             var f=this.getField("Spawn 2.1").page;

                                                                                                                 this.deletePages(f);

 

I attached show/hide commands to the Yes/No radio buttons for these questions. To show hide the submit/button - intention is to only have it visible on last page of document which can be page 1, 2 or 3.

Q1 Yes shows P1 footer/submit button, Q2No, hides them. Q2 Yes hides the p1 and 2  items, Q2 No shows the p2 items. This could still end up with two footer/submit buttons visible if the answers to the first two questions were changed. Adding this hidden calculation field fixed that.

 

if(this.numPages>2){

this.getField("P1.Footer").display=display.hidden;

this.getField("P1.Submit").display=display.hidden;

}

else if(this.numPages<2){

this.getField("P1.Footer").display=display.visible;

this.getField("P1.Submit").display=display.visible;

}

try67
Community Expert
March 12, 2024

Your code doesn't cover the scenario where the file has exactly two pages. Is that on purpose?

Ozkim24Author
Inspiring
March 12, 2024

It's covered by the radio button show/hides but I couldn't get that to cover every outcome, the additional hidden code gives me the extra option I needed. Aware there are probably more elegant ways of doing this but they were beyond me and this does seem to work!

Ozkim24Author
Inspiring
February 28, 2024

Thanks for the tips so far, it's massively appreciated. I only do a few of these jobs a year and don't have any in-house support!

I've got the spawn/delete spawn working now as follows:
Q1: Yes:                                                                                                    Q1: No:
var a_this.getTemplate("Spawn 1");                                                     var f=this.getField("Spawn 1.1").page;

a.spawn({nPage:1,bRename:false,bOverlay:false})                            this.deletePages(f);

Q2: Yes:                                                                                                    Q2: No:
getTemplate("Spawn2").spawn(this.numPages,false,false);             var f=this.getField("Spawn 2.1").page;

                                                                                                                 this.deletePages(f);

But I'm still stuck on the final page show/hide command - will keep digging and post any results but if anyone has any pointers they would be hugely appreciated. Currently thinking a hidden calculate field may be the way to go, but not sure if it will need triggered by something else.

Thom Parker
Community Expert
February 29, 2024

So is the Button/footer on the spawned page? If so, then use the state of the check boxes.  The best option is to use a calculation on a hidden field. It needs to include hiding all the fields first, so changes in the templates are captured. 

  

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
March 5, 2024

Hi Thom, thanks for responding. I've had a go at building a script but I'm not getting it right. Javascript  debugger is telling me there’s a ; missing at end of second statement so I know I’ve got something wrong

There's two Yes/No radio button questions with "Yes" "No" values attached to buttons - these spawn/delete p2 and p3 - which is working fine but I'm struggling with getting the footer and submit button to show on the correct page:

If both questions are No the footer/button is only visible on p1 (and only p1 is visible)

If Q1 is Yes, and Q2 is No the footer/button is only visible on p2 (and p1 and p2 are visible)

If Q1 is No, and Q2 is Yes the footer/button is only visible on p3 (and p1 and p3 are visible)

If both questions are Yes the footer/button is only visible on p3 (and all 3 pages are visible)

Footer and submit are set-up as visible on p3 and hidden on p1 and 2.

 

I’ve got a hidden button on p1 to plug these calculations into - my failing script:

//Defining the two YN Questions

var c1=this.getField(“Allocation YN”).valueAsString;

Var c2=this.getField(“WA YN”).valueAsString;

//Defining the fields to show/hide

var F1 = this.getField("P1 Footer");

var F2 = this.getField("Submit_P1");

var S1 = this.getField("P2 Footer.1”);

var S2 = this.getField("Submit_P2.1”);

 //attempt at show and hide logic

if(c2!=“Yes”){

F1=display.hidden;

F2=display.hidden;

S1=display.hidden;

S2=display.hidden;

 

if(c1!=“Yes” && c2!=“No”){

F1=display.hidden;

F2=display.hidden;

S1=display.visible;

S2=display.visible;

 

if(c1!=“No” && c2!=“No”){

F1=display.visible;

F2=display.visible;

S1=display.hidden;

S2=display.hidden;


You must only use straight quotes in your code ( " ... " ), not curly ones.

Bernd Alheit
Community Expert
February 28, 2024

Info:

var f = this.getField("Spawn 1").page;

will give you an array of pages.

try67
Community Expert
February 28, 2024

You need to check the number of pages in the file using this.numPages, just like you did in your second script, and if it's not 1, delete all the pages after the first page, like this:

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

Also, you shouldn't be using spawnPagesFromTemplate, but the Template object's spawn method.