Avatar of Julie McFadden
Julie McFadden
Flag 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

JavaScriptPDF

Avatar of undefined
Last Comment
Julie McFadden

8/22/2022 - Mon
Julie McFadden

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.
Julie McFadden

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.
 
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
Julie McFadden

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck