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

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

40.1K

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;

}

Votes

Translate

Translate
Community Expert ,
Mar 13, 2017 Mar 13, 2017

Copy link to clipboard

Copied

Use something like this:

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

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

} else {  

...

}

Votes

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

Lol, this is frustrating. 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);

} else {  

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

}

It says SyntaxError: unterminated string literal 2: at line 3

Votes

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

Copy link to clipboard

Copied

If this is your actual code you need to remove the line-break in the middle of the alert text.

Votes

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

I'm so sorry lol, can you please post the corrected code? I'm really new to JavaScript and don't know how to break the line in the middle?

Votes

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

Copy link to clipboard

Copied

You need to "un-break" it...

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

} else {

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

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

        if (f==null) continue;

        f.readonly = true;

    }

}

Votes

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

You are a genius Try67! Thank you SO much for your help!! It works perfectly!!

Votes

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

Just another quick question, I would like my reset button to present a pop-up alert. Is there any way I can do this while having two actions?

Right now, one action for the button is set to Mouse Up- Reset entire form and another action set to Mouse Up- Run a JavaScript with the following:

var svfile = app.alert ("You are about to reset the form. Are you sure?", 1, 1);

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

    app.alert ("Please review and reset.", 1);

}

I notice when I click on the button, the form resets before the prompt message allows me to erase the data.

Is there a way I can present a prompt message before the form resets?

Thanks so much!!

Votes

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

Copy link to clipboard

Copied

Yes, but if you want to allow the user not to reset the form after the message is displayed then you need to do it all using a script.

The command to reset a form is simply this:

this.resetForm();

Votes

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
Explorer ,
Mar 28, 2018 Mar 28, 2018

Copy link to clipboard

Copied

Hi Try,

Is there a way that you could slightly alter this bit of code to look at each of the fields on your form but only make fields that have something in them read only?

Cheers,

Seb.

Votes

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 Expert ,
Mar 29, 2018 Mar 29, 2018

Copy link to clipboard

Copied

Before this line:

f.readonly = true;

Add this:

if (f.valueAsString!="")

Votes

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 ,
Mar 18, 2020 Mar 18, 2020

Copy link to clipboard

Copied

Jay, I'm a serious newbie and I'm attempting something similar but also wish to include a popup warning meesage with a No/Yes button to proceed or back out.  This is what I have (which I pieced together from two different scripts)

 

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

{

if (app.alert({nType:2,nIcon:2,cMsg:"This will lock the document for printing and submission. Are you sure you want to continue?"})==4)

{

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

}

..............?

 

From here I'm not sure what would be next.  I've tried several different ideas, all resulting in the window staying open regardless of clicking  "Yes" or" No", forcing me to force quit Adobe.  

 

FYI, the pop up was hijecked from a checkbox script and the fields to readonly portion was from a Button script.

 

Votes

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 ,
May 04, 2020 May 04, 2020

Copy link to clipboard

Copied

LATEST

this Code works and makes all fileds read only. thanks 

 

if (app.alert("Are you sure you want to Save this form?",2,2)==4)

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

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

if (f==null) continue; 

f.readonly = true; 

 

the only drawback here is the person filling the PDF has to save it twice once by clicking the above javascript button for readonly fields and the second time in file save as to save the file and people usually miss clicking the button.

 

my file has 3 buttons

- 1 for resetting all the fields.

-2 for Saving all the fields as read only (the above script that works)

- 3 for emailing the filled form with the below script.

 

my problem here is that if I follow button 2 then the 3 button becomes readonly (is there a way i can merge the save and email option the above and below script)

 

var url = "mailto:?subject= &body= .";
this.submitForm({cURL: url, cSubmitAs: "PDF"})

Votes

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 ,
Jul 18, 2017 Jul 18, 2017

Copy link to clipboard

Copied

This is a great thread..I've learned a lot, but I haven't been able to solve my problem...sorry I'm a JS newbie.

I have a form with a reset button. Several calculated fields are marked as read only. I’m having trouble with the actions a “Clear Button” executes in the form.

Under the Button Properties \ Actions tab if have two actions associated with the button – the first is a JavaScript that does the following:

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

{

this.getField(this.getNthFieldName(cntr)).readonly = false

};

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

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

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

//this.getField("XORG").readonly = true;

//this.getField("EMPNAME").readonly = true;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

There are more fields than this....

The second action is “Reset a form” – with all the fields in the form “checked”.

Here’s my problem – when the “Clear Form” button is pressed, it resets the form as expected, but all of the fields I expect will be set to “readonly” are now open for edit – and a user can type into the field.

Any suggestions on how to reset the form, and still have the readonly fields “protected”?

Votes

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 Expert ,
Jul 18, 2017 Jul 18, 2017

Copy link to clipboard

Copied

That should not happen... The two things are not related.

What I would recommend you do is delete the second action and do it in your

script, instead.

Simply add this line of code at the end of it:

this.resetForm();

On Tue, Jul 18, 2017 at 8:53 PM, jg84996200 <forums_noreply@adobe.com>

Votes

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 ,
Jul 18, 2017 Jul 18, 2017

Copy link to clipboard

Copied

Thanks for the quick replay try67.

I eliminated the second action - "Rest a form" - and replaced it with the "this.resetForm();" statement at the end of the JS code. However, now the form fields don't reset, and I'm getting the same result - with the read only fields - as I did before.

I inherited this form and code from another programmer.

From what I've read, a form reset should only affect the contents of a field, not it's attributes / settings, (i.e., "read only").

If I'm understanding the code correctly - it first set's the "read only" state of all of the listed fields to false (or no),

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

  {

    this.getField(this.getNthFieldName(cntr)).readonly = true

....and then using the counter loops it through the fields again and sets the read only state of the fields to the indicated "= true" (or yes).

I'm wondering...should the "reset form" come before the code that set's the read only state of the field to "no" and "yes"? Just a guess. Doesn't seem right - now that I'm writing it - seems that the state needs to be set to false before resetting the field and then setting the state back to "read only" to protect it again.

Votes

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 Expert ,
Jul 18, 2017 Jul 18, 2017

Copy link to clipboard

Copied

You're correct. Resetting the form doesn't affect any properties of the fields, except their values. All it does is revert the fields to their default values. It shouldn't matter where you place the resetForm command. It should work correctly either before or after the rest of the code, but maybe somewhere there's another piece of code that's enabling the fields, triggered by the reset command.

So try moving it to the top of the code and see if that helps.

Also, make sure there are no errors in the JS-Console (Ctrl+J) after you run it.

Votes

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 ,
Jul 18, 2017 Jul 18, 2017

Copy link to clipboard

Copied

Thanks. I'll give that a try...and report back. Thanks for taking the time

to review this.

Votes

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