PDF Form javascript for making readonly field by using button

Guest
Oct 08, 2014 Oct 08, 2014

Copy link to clipboard

Copied

Please let me know the PDF Form JavaScript for making selected fields(Text field,Drop down list,..) as read-only by using Button.

TOPICS
PDF forms

Views

29.1K

Likes

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

correct answers 1 Correct answer

LEGEND , Feb 09, 2015 Feb 09, 2015
Do you want your button to be made read only?One starts with Acrobat JS Reference.// make all fields in a form read only;var oField; // variable for field being processed;// loop through the form fields;for (var i = 0; i < this.numFields; i++) {// process each field name;oField = this.getField(this.getNthFieldName(i)).readonly = true;}

Likes

Translate

Translate
Adobe Community Professional ,
Oct 08, 2014 Oct 08, 2014

Copy link to clipboard

Copied

this.getField("FieldNameHere").readonly = true;

Likes

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
Guest
Oct 08, 2014 Oct 08, 2014

Copy link to clipboard

Copied

Thanks Mr.Gilad D, its working fine.

Likes

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
Community Beginner ,
Feb 09, 2015 Feb 09, 2015

Copy link to clipboard

Copied

I know this an old post, but I was wondering if there was an all fields command for this? I used that line effectively to lock one field, but can I change the field name to something that does all of the fields? Thank you!

Likes

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 ,
Feb 09, 2015 Feb 09, 2015

Copy link to clipboard

Copied

‌Create a loop over all fields.

Likes

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
Community Beginner ,
Feb 09, 2015 Feb 09, 2015

Copy link to clipboard

Copied

Can you please explain how to do this? I have searched creating a loop and I have found a few different articles but I can't find the proper code to insert into the javascript box that comes up with the button.

Likes

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
LEGEND ,
Feb 09, 2015 Feb 09, 2015

Copy link to clipboard

Copied

Do you want your button to be made read only?

One starts with Acrobat JS Reference.

// make all fields in a form read only;

var oField; // variable for field being processed;

// loop through the form fields;

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

// process each field name;

oField = this.getField(this.getNthFieldName(i)).readonly = true;

}

Likes

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
Community Beginner ,
Feb 12, 2015 Feb 12, 2015

Copy link to clipboard

Copied

That works perfectly! Thank you very much.

Likes

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 ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

Is it possible to use the script below for more fields in a form?

this.getField("FieldNameHere").readonly = true;

For example:

My form have the following fields:

Fieldname-1, Fieldname-2, Fieldname-3, Fieldname-4, Fieldname-5 and Fieldname-6.

Fieldname-1 thru Field-name-4 must be read-on with the javascript. How most i change the script?

Kind regards,

Jarno

Likes

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 ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

You can use following:

for (var i = 1; i <= 6; i++) {

  this.getField("Fieldname-" + i).readonly = true;

}

Likes

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
Community Beginner ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

So thankful for this...

Is there also a possibility to "reverse" this action?

I used this snippet:

// make all fields in a form read only;

var oField; // variable for field being processed;

// loop through the form fields;

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

// process each field name;

oField = this.getField(this.getNthFieldName(i)).readonly = true;

}

and it works great, but I would like to add another button to the document, to undo the first snippet. (For my clients, in case he presses the first button and saves the document by default... )

Thank you all in advance, best regards, Sanny

Likes

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 ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

Use the same code, replacing "readonly = true" with "readonly = false" ...

On Tue, Nov 15, 2016 at 11:10 AM, SannyFuchs <forums_noreply@adobe.com>

Likes

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
Community Beginner ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

Hey try67,

thanks for your fast reply, so great.

I wanted to try it, but after using the button with the first snippet`s JS, it seems that the "buttons" aren´t buttons anymore. So the "redo" Button isn`t working at all, on click both buttons only switch between normal and hover-style. Guess i will have to make sure, that buttons shall NOT be changed in the first snippet. With other words: Put an exception for special fields.(by name/function) or other way round, ONLY TEXTfields shall be read only, but nothing else)

Maybe you have a solution for this, too? (I`m sorry, got no idea about JS)

Thanks in advance.

Likes

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 ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

Yeah, that makes sense... Should have thought of that before.

And yes, of course you can do it. Here's how you ignore specific fields:

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

var fname = this.getNthFieldName(i);

if (fname!="Button1" && fname!="Button2") {

     this.getField(fname).readonly = true;

}

}

Likes

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
Community Beginner ,
Nov 15, 2016 Nov 15, 2016

Copy link to clipboard

Copied

Wohooha, this works! Thank you so much!

Just one thing in addition, maybe its useful for someone else:

I found this snippet, which includes an "alarmfield":

var r = app.alert("Are you sure to do this?",2,2);

if (r == 4) { // 4 is Yes, 3 is No

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

        var fname = this.getNthFieldName(i);

        this.getField(fname).readonly = true; // makes all fields readonly

    }

}

and combined it with your last one, excluding the buttons.

var r = app.alert("Are you sure to do this?",2,2);

if (r == 4) { // 4 is Yes, 3 is No

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

var fname = this.getNthFieldName(i);

if (fname!="Button1" && fname!="Button2") {

     this.getField(fname).readonly = true;

}}

}

It works! Maybe I should learn JS... ^^  Thank you very much, Try67!

Likes

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
Guest
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

My script for Java isn't working for about 100+ fields and is frustrating me! The first field only works as it converts to ReadOnly but the rest don't.

Can someone please tell me what I'm doing wrong?

I'm using this:

var
svfile = app.alert ("You are about to lock the fields. You will not be
able to update your info after the fields are locked. Lock fields?", 1,
1);

if
(svfile == 2 || svfile ==3) {

     app.alert ("Please review your data
and return to this button to lock fields.", 1);

     };

    

if (svfile == 4 || svfile ==1) {

   
this.getField("JobTitle").readonly = true;

this.getField("JobTitle1").readonly = true;

this.getField("JobLevel").readonly = true;

this.getField("Check Box18").readonly = true;

this.getField("Check Box19").readonly = true;

this.getField("Check Box20").readonly = true;

};

Likes

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 ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

Did you create child fields like "JobTitle.1", "JobTitle.2", etc.? If not, why would it edit 100 fields if you only run it on six?

Likes

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
Guest
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

I have over 100 fields and checkboxes with different names but I only pasted 6 for demonstration.

Likes

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 ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

So what is not working correctly, then?

Likes

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
Guest
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

I copied and pasted my code to Word incase my PDF freezes. Basically, I pasted the exact script in the Javascipt action and only certain fields are converted to Read-Only while a majority of them are not. I checked over the field and check-boxes names and they were correct.

I'm not sure why this is happening. When I physically type out 20+ field name in the script coding area, it works. Typing out over 100 will take quite a long time.

Likes

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 ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

Do you want to set all the fields in the file as read-only?

On Fri, Mar 10, 2017 at 9:43 PM, shazias99501584 <forums_noreply@adobe.com>

Likes

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
Guest
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

Yes! I need to set all the fields to Read only upon a prompt message.

Except all the fields, checkboxes, drop-downs are named variously!

I would appreciate a solution!

Likes

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
Guest
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

It's not absolutely necessary to have a prompt message but I do need to create a button that allows users that option

Likes

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 ,
Mar 10, 2017 Mar 10, 2017

Copy link to clipboard

Copied

You can use this code, either with your alert or without it:

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

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

    if (f==null) continue;

    f.readonly = true;

}

Likes

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
Guest
Mar 13, 2017 Mar 13, 2017

Copy link to clipboard

Copied

This code works perfectly thank! How can I add this with the prompt message?

Would it be this:

var
svfile = app.alert ("You are about to lock the fields. You will not be
able to update your info after the fields are locked. Lock fields?", 1,
1);

if
(svfile == 2 || svfile ==3) {

     app.alert ("Please review your data
and return to this button to lock fields.", 1);

     };

{  

for (var i=0; i<this.numFields; i++) {
    var f = this.getField(this.getNthFieldName(i));
    if (f==null) continue;
    f.readonly = true;
}

Likes

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
    • 1
    • 2