Skip to main content
March 21, 2012
Answered

Lock form & email as PDF Javascript

  • March 21, 2012
  • 2 replies
  • 27684 views

Hi Guys,

I am using Adobe Acrobat X and creating a Forms natively. We do not use LiveCycle.

So I have created a form with several fields, buttons a date picker - some of which I have found online.

The forms that I will be creating will be placed on the Intranet of the Organisation that I work for.

So what I need is for our users to fill out the form online. Press the Submit button. When they press submit the form must lock all the contents. We do not want to contents edited when it reaches its destination. The submit button will also call up the default email program, populate the TO: field with a specified email address and the Subject field with specified text.

The form must arrive at its destination in PDF format.

Been stuck on this one for a while. I used some simple html with the submit button for a while and that mostly worked except the part about locking the form.

So if someone can java me up a solution i would greatly appreciate it.

I have tried a few of the solutions a few solutions from the lifecycle partof the forum but apparently the java is different!

Many thanks,

Nick

This topic has been closed for replies.
Correct answer try67

In that case you can only set the fields as read-only, not truly flatten

them. Here you'll find a script I posted that will do that:

http://acrobatusers.com/forum/forms-acrobat/i-need-users-fill-form-and-save-it-then-it-shouldnt-be-anymore-editable/

Regarding submitting the file by email and pre-populating the various

fields, see this:

http://acrobatusers.com/tutorials/dynamically-setting-submit-e-mail-address

The variabt you should use is mailDoc().


I've written an improved version of the code mentioned. This version will completely flatten the file when used in Acrobat, but will just make the fields read-only when used in Reader.

flattenFields()

function flattenFields() {

    if (app.viewerType=="Reader") {

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

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

            if (f==null) continue;

            f.readonly = true;

        }

    } else {

        this.flattenPages();

    }

}

2 replies

January 12, 2015


I would like to know how I can lock only selective fields

February 4, 2016

Here's a script I've been using for years, although it only locks the fields. Any ideas as on how to modify it to submit a form as well?

     for

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

     {var fname = this.getNthFieldName(i);

     var f = this.getField(fname);

     if (f.type!="button") f.readonly = true;}


Inspiring
October 20, 2020

You can use this code to do it. Specify the names of the fields to "skip" (ie. keep editable) in the first line:

 

Edit: Small mistaked in the code fixed.

 

 

var fieldsToSkip = ["Text1", "Radio2", "Checkbox3"];
for (var i=0; i<this.numFields; i++) {
	var fname = this.getNthFieldName(i);
	var f = this.getField(fname);
	if (f!=null && fieldsToSkip.indexOf(f.name)==-1 && f.type!="button") 
		f.readonly = true;
}

 


I amended the script behind my "submit" button with the above, but I am receiving an error message.  Since I know very little about JS, I cannot figure out what is causing the error.  I would appreciate assistance with recommended changes to the following so this will work:

var ok = true;

var i = 0;

var fieldsToSkip = ["RU.Case#", "RU.Department", "RU.Cost Center","RU.Company Code", "RU.Location Address", "RU.FTE", "RU.Job Title", "RU.Reports To", "RU.Hire Date","RU.Other"];
for (var i=0; i<this.numFields; i++) {
var fname = this.getNthFieldName(i);
var f = this.getField(fname);
if (f!=null && fieldsToSkip.indexOf(f.name)==-1) && f.type!="Click to Submit")
f.readonly = true;
try{

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

if(f.required==true){

if(f.value==f.defaultValue && f.type != 'button' && f.type != 'signature'){

ok=false;app.alert('Your Report cannot be submitted because the following field(s) are blank: '+f.name);

}

}

}catch(ex){};

};

if(ok==true){

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

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

f.readonly = true;

};
var fld = this.getField("Date Signed");

fld.value = util.printd("mmmm dd, yyyy HH:MM:ss",new Date());

var to = "emailme@domain.com";

var strUrl = "mailto:"+to+"?subject=Report&body=Attached is my completed Report&cc=&bcc=";

var submitAs = "PDF";

this.submitForm({cURL: strUrl, cSubmitAs:"PDF"});

};

try67
Community Expert
March 21, 2012

Is the file going to be used in Acrobat or in Reader?

March 21, 2012

The users filling the form out will be mixed. Some will have writer, others will have Reader only.

Thanks for the swift response

try67
Community Expert
March 21, 2012

In that case you can only set the fields as read-only, not truly flatten

them. Here you'll find a script I posted that will do that:

http://acrobatusers.com/forum/forms-acrobat/i-need-users-fill-form-and-save-it-then-it-shouldnt-be-anymore-editable/

Regarding submitting the file by email and pre-populating the various

fields, see this:

http://acrobatusers.com/tutorials/dynamically-setting-submit-e-mail-address

The variabt you should use is mailDoc().