Copy link to clipboard
Copied
I have a code to mark fields on my pdf as read only. I would like 3 of the fields to remain active if they are empty. How do I adjust the code to exclude 3 fields if they are empty?
var currentPageNum = event.target.page;
for (var i = 0; i < this.numFields; i++){
var currentField = this.getField(this.getNthFieldName(i));
if (GetFirstFieldPage(currentField) <= currentPageNum){
currentField.readonly = true;
currentField.display = display.visible;
}
}
I tried the changes below, but I couldn't get it to work.
var currentPageNum = event.target.page;
var activeFields = ['field1', 'field2', 'field3']; // Names of the fields that should remain active if empty
for (var i = 0; i < this.numFields; i++) {
var currentField = this.getField(this.getNthFieldName(i));
if (GetFirstFieldPage(currentField) <= currentPageNum) {
var fieldValue = currentField.value;
var isEmptyField = fieldValue === null || fieldValue.trim() === '';
// Check if the field should remain active if it's empty
if (activeFields.includes(currentField.name) && isEmptyField) {
currentField.readonly = false;
currentField.display = display.visible;
} else {
currentField.readonly = true;
currentField.display = display.visible;
}
}
}
Copy link to clipboard
Copied
Are you getting any error messages? Some of the methods you're using will not work in earlier versions of Acrobat/Reader (such as trim and includes).
Copy link to clipboard
Copied
No error message. None of the fields change to read only.
Copy link to clipboard
Copied
Try to simplify your code. You have a bunch of conditions and functions we can't see and don't know if they work or not. Also, you need to move the definition of the isEmptyField variable outside of the if-statement where it's currently located.
Copy link to clipboard
Copied
This code should make all fields, except for those in the list, read-only:
var activeFields = ['field1', 'field2', 'field3']; // Names of the fields that should remain active if empty
for (var i = 0; i < this.numFields; i++) {
var fname = this.getNthFieldName(i);
if (activeFields.indexOf(fname)!=-1) continue;
var currentField = this.getField(fname);
currentField.readonly = true;
}
Does it work?
Copy link to clipboard
Copied
It locked all fields
Copy link to clipboard
Copied
Then the field names in the array are incorrect. Remember that JS is case-sensitive, and that the names must match EXACTLY, so if the fields are actually called "Field 1" or "field1" instead of "field 1", it won't pick it up.
Copy link to clipboard
Copied
Does anyone has the solution, every eclusion ("All fields except these" and" Just these fields" doesn't work either by providing a script or working pdf
Copy link to clipboard
Copied
The script provided above by try67 will set the fields to read only, except the ones you put in array.
Did you try that script?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Works fine for me, all fields are read only except the 3 fields from the script.
You don't need script for that, try these options:
Copy link to clipboard
Copied
I did that too, it doesn't work for me
Find more inspiration, events, and resources on the new Adobe Community
Explore Now