Link to home
Start Free TrialLog in
Avatar of Julie McFadden
Julie McFaddenFlag for United States of America

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 . . "); } }

Open in new window

Avatar of Julie McFadden
Julie McFadden
Flag of United States of America image

ASKER

I tried adding more pages to the form in an attempt to resolve the range error noted above so I now a total of 24 pages. Funny thing is that Performed by Signature fields appear on pages 2, 8, 14, 17, 19 and 24 however now the buttons is placed, still in duplicate, on pages 1,2, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 18, 19, 20, 23 and 24.
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.
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.
 
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 . . ");
}



Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julie McFadden
Julie McFadden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial