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

Deleting Spawned Pages and final page buttons issue

Explorer ,
Feb 28, 2024 Feb 28, 2024

Copy link to clipboard

Copied

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!

TOPICS
JavaScript , PDF , PDF forms

Views

460

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

correct answers 1 Correct answer

Explorer , Mar 12, 2024 Mar 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

...

Votes

Translate

Translate
Community Expert ,
Feb 28, 2024 Feb 28, 2024

Copy link to clipboard

Copied

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.

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 ,
Feb 28, 2024 Feb 28, 2024

Copy link to clipboard

Copied

Info:

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

will give you an array of pages.

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
Explorer ,
Feb 28, 2024 Feb 28, 2024

Copy link to clipboard

Copied

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.

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 ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

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 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
Explorer ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

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;

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 ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

The "if" statements are missing the closing bracket.  They should also be written with "else if".

You can read about writing "if" statements here:

https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm

 

Here's an update to your code:

 

if(c2!=“Yes”){

  F1=display.hidden;

  F2=display.hidden;

  S1=display.hidden;

  S2=display.hidden;

} 

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

  F1=display.hidden;

  F2=display.hidden;

   S1=display.visible;

  S2=display.visible;

 }

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

  F1=display.visible;

  F2=display.visible;

  S1=display.hidden;

  S2=display.hidden;

}

 

 

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
Explorer ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

Thanks Thom, I really appreciate your efforts, you should have a tip jar!

I've tried that but unfortunately it's not doing anything. No error in code in javascript debugger but none of the footers/buttons are being affected at all. I tried moving calculation field to top of calculation order. I tried building scripts into the radio buttons as well so that each yes/no choice triggered a show/hide depending on answer in other question but that doesn't work either so I'm wondering if I've defined something wrong at the start?

The two questions this is all triggering from are radio buttons with the 'Choices' set to Yes and No in the Radio Button Properties - does that change anything? 

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 ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

I missed another problem with your code. The valiables are fields, so it is the display property that needs to be set

 

if(c2!="Yes"){

  F1.display=display.hidden;

  F2.display=display.hidden;

  S1.display=display.hidden;

  S2.display=display.hidden;

} 

else if(c1!="Yes" && c2!="No"){

  F1.display=display.hidden;

  F2.display=display.hidden;

   S1.display=display.visible;

  S2.display=display.visible;

 }

else if(c1!="No" && c2!="No"){

  F1.display=display.visible;

  F2.display=display.visible;

  S1.display=display.hidden;

  S2.display=display.hidden;

}

 

 

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
Explorer ,
Mar 11, 2024 Mar 11, 2024

Copy link to clipboard

Copied

Thanks again Thom, sadly this still isn't working for me. I started from the beginning again and built a page calculation field gives the number of pages in the document (can only be 1, 2 or 3 for). I used this as a reference for the show-hide commands and have nearly got it working.  The initial radio buttons still spawn and delete pages (they have the Pages calculation attached so it always updates). If document is 2 or 3 pages everything works correctly and when it is at base 'open' state with all fields clear it works correctly (p1 footer/submit visible). But if pages 2/3 are deleted (via radio button options) and page count returns to 1 the p1 footer/button is not visible.

Current hidden calculation field code:

var c1=this.getField("Pages").valueAsString;
var f1=this.getField("P1Footer");
var f2=this.getField("SubmitP1");
var s1=this.getField("P2Footer");
var s2=this.getField("SubmitP2");

 

if (this.getField("Pages").value.length == 0) {
f1.display=display.visible;
f2.display=display.visible;
}

else if(c1!=2){
f1.display=display.hidden;
f2.display=display.hidden;
s1.display=display.hidden;
s2.display=display.hidden;
}
else if(c1!=1){
f1.display=display.hidden;
f2.display=display.hidden;
s1.display=display.visible;
s2.display=display.visible;
}
else if(c1!=0){
f1.display=display.visible;
f2.display=display.visible;
}



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

Copy link to clipboard

Copied

I think you are moving in the right direction, but you need to do some debug to see the details of what's going on with the form in the different situations.  Use the console window to display the values of the field of interest and to test code. Here's a video on the topic. 

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm

 

Also, the number of pages is always available in the 'doc.numPages' property. This is no need to use a field to capture it. 

 

 

 

 

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
Explorer ,
Mar 11, 2024 Mar 11, 2024

Copy link to clipboard

Copied

For anyone else struggling with this I ended up splitting it. Put as much show/hide as I could into the radio buttons to ensure there is always a footer and submit button visible in the document. I'm sure there's more elegant ways of doing this but I am out of time on this one. (I've changed field names over last few attempts if anyone is wondering why they're not the same throughout.)

 

I've added this to the hidden calculation field:

if(this.numPages<2){

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

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

}

else if(this.numPages>2){

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

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

}

 

It seems to be doing what I need although if the initial radio buttons that spawn the pages get changed from Yes to No too many times I get a 'Bad Parameter' warning. Nothing in javascript console so customer will just have to live with random warning! I just could not get it working properly when I specified the actual page count.

 

Thanks to everyone who tried to 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 ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

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

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
Explorer ,
Mar 05, 2024 Mar 05, 2024

Copy link to clipboard

Copied

Thanks Try67! I do have straight quotes in the actual code but can't access this forum from my work machine and typing it in on my phone seems to default to curly quotes.

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
Explorer ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

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;

}

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 ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

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

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
Explorer ,
Mar 12, 2024 Mar 12, 2024

Copy link to clipboard

Copied

LATEST

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!

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