Skip to main content
Inspiring
March 26, 2024
Answered

Spawn pages / delete pages depending on checkbox

  • March 26, 2024
  • 1 reply
  • 1533 views

I have a 4 page assessment - when completed it has a button to generate a 2 page report. One of the report's pages is called 'report 1', the other is called 'spasticity'. If a checkbox on the final page of the assessment is checked, 'spasticity' should be spawned, if not checked, 'report1' should be spawned. The other page in the report should be spawed either way. The following step is to print these 2 pages as a separate PDF, then delete both pages, leaving the 4 page assessment. I've tried an if/else I thought would be simple but cannot get it to work. Any advice would be great, thanks!!

This topic has been closed for replies.
Correct answer stephenf87341748

That behavior would indicate that the "if" statement is always returning "false".  Or that there is some other issue, such as the templates are named incorrectly. So, some debug is needed. 

The first thing I'd do is run each line of the spawn code individually in the console window to ensure the templates are what you think they are. 

Then I'd run the condition code in the "if" statement to see what it returns when the checkbox is checked and not checked. 

 

 

 


Thanks for all your help Thom - finally sorted it (wouldn't have got there without your advice). I was using the checkbox value "Yes" to spawn and the Export Value for the checkbox was always "Yes" so exported the same templates regardless. In the end, updated the script to the below and now working OK.

var checkbox = this.getField("spasrep");
var templateChecked = this.getTemplate("spasticity");
var templateUnchecked = this.getTemplate("report1");
var templatemeasure = this.getTemplate("report2");

if (checkbox.isBoxChecked(0)) {
templateChecked.spawn();
templatemeasure.spawn();
templateUnchecked.remove();
} else {
templateUnchecked.spawn();
templatemeasure.spawn();
templateChecked.remove();
}

1 reply

Thom Parker
Community Expert
Community Expert
March 26, 2024

So are the  'report 1' and 'spasticity' pages already setup as page templates and hidden?  If so, are those the "template" names? Why are they being spawned and then deleted? Maybe there is a better way to accomplish your goal?

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
March 27, 2024

Yes, there are three report pages set up as templates and hidden: 'report1',  'report2', 'spasticity' (they are the template names). The idea is once the first 4 pages are completed (this is the assessment), the user should click a button to generate a report (includes 2 template pages), and the next step is that the print panel opens and is set to print just the 2 report pages and then delete these pages from the file. So the assessment ends up being 1 file and the report a separate file. I did have the print/delete javascript working correctly when the report didn't require a separate page based on the checkbox. Hope that makes some sense. Thanks

Thom Parker
Community Expert
Community Expert
March 27, 2024

Sounds like you are close.  Could be a simple error with the "if". So two more questions. 

Are any errors reported in the Console Window (Ctrl-J), and can you post the (non-working) code you are using?

 

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