How to flatten a specific group of form fields on a page?

I'm testing this script to activate and deactivate a group of form fields on a page and it works great.

(function () {

// Get one of the fields in the group
var f = getField("private.name");

// Determine new readonly state, which
// is the opposite of the current state
var readonly = !f.readonly;

var readonly_desc = readonly ? "deactivate" : "activate";

// Ask user for password
var resp = app.response({
cQuestion: "To " + readonly_desc + " the fields, enter the password:",
cTitle: "Enter password",
bPassword: true,
cLabel: "Password"
});

switch (resp) {

case "panda": // Your password goes here
getField("private").readonly = readonly;
app.alert("The fields are now " + readonly_desc + "d.", 3);
break;

case null : // User pressed Cancel button
break;

default : // Incorrect password
app.alert("Incorrect password.", 1);
break;
}

})();

Is there a similar script to flattened a specific group of form fields on a page?



Thank you,


Rey Hernandez


2 Answers

You can flatten specific fields but you can't toggle back and forth like you do with your readonly script because once the fields are flattened they no longer exist. The flatten function flattens all annotations (including form fields) on a page or a range of pages. This function has an optional input parameter to exclude annotations that are non-printable so in order to flatten specific fields you would have to write a custom script that pushes the visibility of all annotations into an array, sets all the fields you want to exclude to non-printable, flattens all pages, then returns the remaining fields to their original visibility. Or you can download a tool that does all of this for you from www.pdfscripting.com.


David Dagley   

Supposing the PDF form has no comments or annotations, just form fields, you can use a script such as the following to implement the procedure described by David:

var list = new Array(); 
list = [ //fields to be flattened
    "personalInfo",
    "notes",
];

var fields = new Array;
for (var i = 0; i < this.numFields; i++) {
    fields[i] = [
        this.getNthFieldName(i),
        this.getField(this.getNthFieldName(i)).display
    ];
    this.getField(fields[i][0]).display = display.noPrint;
}

for (var i = 0; i < list.length; i++) {
    this.getField(list[i]).display = display.visible;
}

this.flattenPages({
    nStart: 0,
    nEnd: this.numPages - 1,
    nNonPrint: 1,
});

for (var i = 0; i < this.numFields; i++) {
    for (var j = 0; j < fields.length; j++) {
        if (fields[j][0] == this.getNthFieldName(i)) {
            this.getField(this.getNthFieldName(i)).display = fields[j][1];
        }
    }
}


Almir R V Santos   


Please specify a reason: