Create a button to remove all buttons in document

New Here ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

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. 

TOPICS
JavaScript, PDF forms

Views

92

Likes

translate

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
Most Valuable Participant ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

Change:

if (f.type = "button")

To:

if (f.type == "button")

Likes

translate

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
Most Valuable Participant ,
Jan 13, 2021 Jan 13, 2021

Copy link to clipboard

Copied

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

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

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

Likes

translate

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
Most Valuable Participant ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Did you change the loop, too? Post your new code.

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Sorry I didn't change the loop yet. I wasn't completely sure what that change would be. Here is what i have currently. 

 

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

Likes

translate

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
Most Valuable Participant ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Change this:

for (i=0; i<this.numFields; i++)

To:

for (i=this.numFields;-1; i>0; i--)

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Thank you, 

I ammeded the code to this

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

when i attempted to cick ok i get the error, "SyntaxError: missing ) after for-loop control 1: at line 2" so I removed the ; before the -1 (saw it like that someplace else) and attemped to run it and still got the Secuirty error.

Likes

translate

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
Most Valuable Participant ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Use this:

 

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

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

I copied and pasted exactly and I am still getting the secuirty error. 

This is what i have. 

for (var i=this.numFields-1; i>0; i--) { 

var f = this.getField(this.getNthFieldName(i)); 

if (f.type == "button") {  

this.removeField(this.getNthFieldName(i)); 

} 

} 

 

Likes

translate

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
Most Valuable Participant ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

A security error or a syntax error? Post the exact text of the error you're getting, please.

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

NotAllowedError: Security settings prevent access to this property or method. 
Doc.removeField:8: 

Likes

translate

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
Most Valuable Participant ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Then something in the file is preventing you from removing those fields. Are you able to do it manually?

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Ya, I created the form from a word document and added a "New Page Button" and the "Save Button" So ya i can go into pepare form and right click on it and delete it. 

 

I will create a new form from a blank page and see it allowes me to delete the buttons. I will post my results. 

Likes

translate

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
Most Valuable Participant ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

There's a small error in the code above, but it shouldn't cause this error you're getting.

In the loop it needs to be:

i>=0

Not

i>0

 

I tested it on a file I created with three buttons and it work just fine.

Here's the code I used:

 

for (var i=this.numFields-1; i>=0; i--) { 
	var fname = this.getNthFieldName(i);
	var f = this.getField(fname); 
	if (f!=null && f.type == "button") {  
		console.println("delete " + fname);
		this.removeField(fname); 
	} 
} 

This will help you find out which field is causing the error.

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

Ok, i copied and pasted that into a new form with 2 buttons and this is what i got 

 

delete Button3 
delete Button2 

NotAllowedError: Security settings prevent access to this property or method. 
Doc.removeField:6:

Likes

translate

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
Most Valuable Participant ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

There's some kind of issue with that field, then.

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

I get the same results starting fom a blank form.

Likes

translate

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
Adobe Community Professional ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

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.

 

Likes

translate

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
New Here ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

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. 

Likes

translate

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
Adobe Community Professional ,
Jan 14, 2021 Jan 14, 2021

Copy link to clipboard

Copied

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

Likes

translate

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
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

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

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

Likes

translate

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
Adobe Community Professional ,
Jan 25, 2021 Jan 25, 2021

Copy link to clipboard

Copied

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

Likes

translate

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