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

Spawn pages / delete pages depending on checkbox

Participant ,
Mar 25, 2024 Mar 25, 2024

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!!

TOPICS
Create PDFs , JavaScript , Modern Acrobat , PDF , PDF forms
1.4K
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
Participant ,
Mar 27, 2024 Mar 27, 2024

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

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
Community Expert ,
Mar 26, 2024 Mar 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 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 ,
Mar 26, 2024 Mar 26, 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

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

Thanks Thom -

The below code works when I just had to generate the same 2-page report regardless of checked boxes:
Spawn report
this.spawnPageFromTemplate("report1",this.numPages, false, false);
this.spawnPageFromTemplate("report2",this.numPages, false, false);

Print just report

var firstPage = 5;
var lastPage = 6;
this.print({nStart: firstPage-1, nEnd: lastPage-1});

Delete report pages

this.deletePages({nStart: this.numPages-2, nEnd: this.numPages-1});

Below is the code that's not working when trying to spawn based off checkboxes:
if (this.getField("spasrep").value != "Off") {
this.getTemplate("report1").spawn();
this.getTemplate("report2").spawn();
}
else {
this.getTemplate("spasticity").spawn();
this.getTemplate("report2").spawn();
}

This is the error reported

InvalidArgsError: Invalid arguments.
printParams.print:5:Field reportword:Mouse Up

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

The error code makes no sense.  It's saying the print parameters are invalid, probably the page range. But your new code does not inlcude the print. It's acting as if it is still running the old code, where the print is on line 5. 

Seems like there is some old code hanging about somewhere.   

 

 

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

Still bothering you - you were right, there was some old code after checking the Javascripts so removed that and not getting an error any longer. Issue is still when using the 'spawn' code, regardless of checking the 'spasticity' checkbox, it still spawns and prints the 'spasticity' template. Would it be something to do with the ordering of the templates (e.g. would it just print the last 2 pages of the document anyway)?

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

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. 

 

 

 

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

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

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 ,
Apr 02, 2024 Apr 02, 2024
LATEST

I'm sure this code is also reporting errors in the Console window. 

There is no "template.remove()" function. 

 

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