Skip to main content
Participating Frequently
January 13, 2021
Question

Create a button to remove all buttons in document

  • January 13, 2021
  • 5 replies
  • 3001 views

Hello, I am attempting to create a form with several fields that the user will fill out and that will have a button that can spawn a additional pages. Each page will have a "Add Page" Button on them. I got that part figured out.
Next I want to create a 2nd button on the first page that will remove all of the buttons including itself, Flatten the document and Save the finalized document so that all the user information typed into the fields is now permeant.
I have attempted to use :

for (i=0; i<this.numFields; i++)
{
var f = this.getField(this.getNthFieldName(i));
if (f.type = "button")
{
this.removeField(this.getNthFieldName(i));
}
}

and I also attempted 

this.removeField("New Page")
this.remvoeField("Save_Button")

So the 2nd code removes the "New Page" button but doesn’t remove it from all of the spawned pages.
but it also fails to remove the "Save_Button" and both codes give the error.

"NotAllowedError: Security settings prevent access to this property method.

Doc.removeField:2:"

 

Any help would be greatly appreciated. 

This topic has been closed for replies.

5 replies

Bernd Alheit
Community Expert
Community Expert
January 25, 2021

Don't use a button for this. Use a link with the script.

Thom Parker
Community Expert
Community Expert
January 25, 2021

I would suggest hiding the buttons, rather than deleting them. There are no security issues with this.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
JR Boulay
Community Expert
Community Expert
January 14, 2021

Beware, flattenPages() does not work in Acrobat Reader!

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
Community Expert
January 14, 2021

I would replace

if (f.type == "button")

 

By

if (f.type == "button" && f.name != event.target.name)

 

Because the script will stop running when the button that is the source of the action will be deleted, even if the loop isn't finished.

And after the loop, when it's finished I would add a single line to delete the button that is the source of the action.

 

Acrobate du PDF, InDesigner et Photoshopographe
Participating Frequently
January 14, 2021

I added what you have suggested however It doesnt remove the source button which i think from reading  your responce is expected. I added a line to remove it.

this.removeField("P0.New_Back_Cert.Save"); 

However this is still resulting in the same secuirty error.

 

Thank you for the warrning about flatting pages, it shouldnt be a issue everyone who will be editing this form will have the full version of Adboe DC. 

try67
Community Expert
Community Expert
January 13, 2021

Change:

if (f.type = "button")

To:

if (f.type == "button")

try67
Community Expert
Community Expert
January 13, 2021

I would also recommend changing the loop to run from the last index to 0.

Participating Frequently
January 14, 2021

Thanks, I added in the 2nd = however i am still getting the error message.

"NotAllowedError: Security settings prevent access to this property method.

Doc.removeField:2:"

 

From what I understand this error comes up because the button is attempting to remove it self. 

Thanks