Julie McFadden
asked on
JavaScript to parse all PDF form fields and add a button to the page when a certain field name found
I have a 6 page PDF form. Pages 1, 4 and 5 each have a pair of signature fields with names that start like “Performed by Signature” and “Reviewed by Signature”. Using JavaScript, I am attempting to parse all the fields on the form and if the field’s name includes Performed AND Signature than add a button on the form.
There are two problems, first the code is placing the button twice on pages and 1 and 4 then throws below error on the console and no button is placed on page 5. Error on console is:
RangeError: Invalid argument value.
Doc.addField:8:Batch undefined:Exec
This effort has maxed my skills and I am now at a loss to fix the problem. Really appreciate any help!
for (var i=0; i<this.numFields; i++) {
var fname = this.getNthFieldName(i)
var f = this.getField(fname);
if (f.name==null) continue;
if (/Performed And Signature/.test(f.name));{
// Make the buttons
var pp = addField("Button_"+(f.page), "button", f.page,[ 12, 100, 155, 60 ] );
pp.buttonSetCaption("Check & Sign Test Case");
pp.strokeColor = color.gray;
pp.borderStyle = border.b;
pp.linewidth = 11;
pp.fillColor=color.blue;
pp.textSize = 12;
pp.textColor = color.white;
pp.setAction("MouseUp", "Insert script here . . ");
}
}
ASKER
Back to the 6 page document to reduce the fields I am dealing with . . . added an app alert to show me the field name, type and page as it was processed. From there I started to eliminate field types and got down to only signature fields and now getting two buttons on the expected pages. Progress! Next, I changed the code so the button got named same as field name and discovered that field was being added for both performed by and reviewed by signature fields. From there I worked on the test of field name and improved that. Also removed a set of curly brackets and success.
My process might not be pretty but I am sharing and posting my final clean code below.
My process might not be pretty but I am sharing and posting my final clean code below.
for (var i=0; i<this.numFields; i++) {
var fname = this.getNthFieldName(i)
var f = this.getField(fname);
if (f.name==null || f.type=='button'||f.type=='radiobutton'||f.type=='text') continue;
if (f.type=='signature' && /Performed/.test(f.name))
// Make the buttons
var pp = addField("Button_", "button", f.page,[ 12, 100, 170, 68 ] );
pp.buttonSetCaption("Check & Sign Test Case");
pp.strokeColor = color.gray;
pp.borderStyle = border.b;
pp.linewidth = 11;
pp.fillColor=color.blue;
pp.textSize = 12;
pp.textColor = color.white;
pp.setAction("MouseUp", "Insert script here . . ");
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Still not able to understand where I am going wrong.
Ultimately, this form could be 100+ pages and to troubleshoot I reduced to 6 and with the increase to 24 I see no reason for all these buttons in duplicate.
Not super skilled in JavaScript so still hoping an Expert can see where I went wrong.