Skip to main content
Inspiring
November 10, 2023
Question

How to add pages conditionally to a checkbox being checked ?

  • November 10, 2023
  • 1 reply
  • 3411 views

Hi,

I have a pdf form document in which there's checkboxes that add a page after a certain page based by their number in the order of appearance. My code is : 

 

var a = this.getTemplate ("AddPage11");

a.spawn(3, false, false);

this.pageNum = 1

 

And it works. But I have several checkboxes like this and so for the subsequent checkboxes that add pages, the order of appearance is conditional to the choice that the user made by clicking on a prior checkbox. Is there a way that a javascript can add pages conditionally to a prior checkbox adding pages being checked ?

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
November 10, 2023

You could enable/disable or show/hide the checkboxes based on previous checkbox values. 

Are the pages also deleted if the checkbox is turned off? 

If there are several templates and associated checkboxes, then this process of spawning and deleting will need to be managed carefully. For example after two pages are spawned, the first checkbox may need to be blocked so the first page is not deleted before the second page. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
November 10, 2023

No, the pages are neither deleted, nor hidden. In fact, when you click on a check box, it creates a template page saved with the function Organize Pages. However, if I click on the checkbox and it creates a page after the first page, every other page that were already visible on the document move from one page, because the new page is the second page now. If on page 4, there's a checkbox to add a page after page 7, page 7 is now page 8, because of the page that got added, so the page that was added after page 7 is now shifted.

Thom Parker
Community Expert
Community Expert
November 10, 2023

You misunderstand me.

If it is necessary to control the order of page spawning, then one strategy is to hide the checkboxes that trigger the spawn of a particular page so they cannot be clicked until conditions are right for that to happen. 

 

Alternatively, if the goal is to place the spawned page at a particular location (which may change because of other spawned pages) then markers can be placed on the existing pages to identify the spawn locations.  The best option for a marker is to use a form field.  For example, if the spawned page is to be placed on the page after the page with the checkbox that spawns it, then the checkbox itself is the marker.  

this.getTemplate ("AddPage11").spawn(event.target.page+1, false, false);

 

Or if the target page is somewhere else, a hidden button field could be placed on the page after which the template is to be placed. 

 

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