10 Replies Latest reply on May 16, 2016 1:16 AM by Bernd Alheit

    Add signature field to second occurance of two words (Nth Word) on page

    Jo_2013


      I have a script which searches for two words ("ENG APP") on a page and adds a digital signature field.

       

      The trick is that there are two occurances of these words and the script places the digital signature field on the first ocurrance of the two words.


      I need the digital signature script to be added to the second occurence of the two words.

       

      There has been a counter added to the script, but this then places the field on the single word ("ENG"), it could have to do with the counter counting one word instead of two.


      If anyone can help me modify the script to count two words and the occurences of the two words instead of one word it would be most appreciated.

       

      Script as follows:


      var ckWords; // word pair to test

      var count = 0;

      numWords = this.getPageNumWords(0); // number of words on page

      // loop through the words on page

      for (var j = 0; j < numWords-1; j++) {

      // get word pair to test

      ckWords = this.getPageNthWord(0, j) + ' ' + this.getPageNthWord(0, j + 1); // test words

      if  (ckWords == "ENG APP") {

      count +=1;

      if (count == 2) {

      var q = this.getPageNthWordQuads(0,  j);

      // convert quads in default user space to rotated

      // User space used by links

      m = (new Matrix2D).fromRotated(this,0);

      mInv = m.invert()

      r = mInv.transform(q)

      r=r.toString()

      r = r.split(",");

      var m = this.addField("EngSignature", "signature", 0,

      [r[0], r[1], r[2], r[5]]);

      }

      }

      }

       

      Thank you.

        • 1. Re: Add signature field to second occurance of two words (Nth Word) on page
          try67 MVP & Adobe Community Professional

          The counter is fine. The issue is that you're only using the quads of the first word to calculate the rect for the signature field. You need to convert both words' quads to rect arrays and then merge them. Then you use the merged rect array for the field you're adding.

          This will only work if both words are both on the same line, though, so you might need to take that into consideration.

          I would also add a break command after you've added the field. There's no need to continue scanning the page once you've done that.

          1 person found this helpful
          • 2. Re: Add signature field to second occurance of two words (Nth Word) on page
            Jo_2013 Level 1

            Thank you very much for your advice.

             

            I have added a quad array to merge the first and second word.

            However, the merged array is not working, as the signature field is added to the first word only.

             

            Can you please advise how I can modify the quad array section of the script to work?

            /* Add Signature Fields to Web Times Generated Time Sheet */

            var ckWords; // word pair to test

            count = 0; 

            numWords = this.getPageNumWords(0); // number of words on page 

            // loop through the words on page 

            for (var j = 0; j < numWords-1; j++) {

            // get word pair to test    

            ckWords = this.getPageNthWord(0, j) + ' ' + this.getPageNthWord(0, j + 1); // test words    

            // check to see if word pair is 'ENG APP' string is present    

            if ( ckWords == "ENG APP") {      

            count+=1;

            // increment counter

            if (count == 2) {

            var quadArray = new Array();

            quadArray[0] =  this.getPageNthWordQuads(0, j);

            quadArray[1] =  this.getPageNthWordQuads(0, j + 1);

            // Convert quads in default user space to rotated

            // User space used by Links

            m = (new Matrix2D).fromRotated(this,0);

            mInv = m.invert()

            r = mInv.transform(quadArray)

            r=r.toString()

            r = r.split(",");

            var m = this.addField("EngSignature", "signature", 0,

            [r[0], r[1], r[2], r[5]]);

            break;

            }    

            }

            }

            • 3. Re: Add signature field to second occurance of two words (Nth Word) on page
              try67 MVP & Adobe Community Professional

              That's not quite what I recommended. You should convert both quads to rect arrays, independently, and then merge the two rects.

              1 person found this helpful
              • 4. Re: Add signature field to second occurance of two words (Nth Word) on page
                Jo_2013 Level 1

                Hi try67,

                 

                I have merged the second word (array2) into the first word (array1) by using the java push method.

                 

                The signature field is still on the first word only, can you please advise where the script needs amending?

                 

                Thank you, advice will be most appreciated.

                 

                 

                /* Add Signature Fields to Web Times Generated Time Sheet */

                var ckWords; // word pair to test count = 0;

                numWords = this.getPageNumWords(0); // number of words on page

                // loop through the words on page

                for (var j = 0; j < numWords-1; j++) {

                // get word pair to test   

                ckWords = this.getPageNthWord(0, j) + ' ' + this.getPageNthWord(0, j + 1); // test words   

                // check to see if word pair is 'ENG APP' string is present   

                if ( ckWords == "ENG APP") {     

                count+=1;

                // increment counter

                if (count == 2) {

                var array1 =  this.getPageNthWordQuads(0, j);

                var array2 =  this.getPageNthWordQuads(0, j + 1);

                Array.prototype.push.apply(array1, array2);

                // Convert quads in default user space to rotated

                // User space used by Links m = (new Matrix2D).fromRotated(this,0);

                mInv = m.invert()

                r = mInv.transform(array1)

                r=r.toString()

                r = r.split(",");

                var m = this.addField("EngSignature", "signature", 0,

                r[0], r[1], r[2], r[5]]); break;

                }   

                }

                }

                • 5. Re: Add signature field to second occurance of two words (Nth Word) on page
                  try67 MVP & Adobe Community Professional

                  Read my replies more carefully. Instead of merging the two sets of quads to a single one, convert each one of them to a rect independently and then combine those rects.

                  1 person found this helpful
                  • 6. Re: Add signature field to second occurance of two words (Nth Word) on page
                    Jo_2013 Level 1

                    I have modified the script to convert the quads independently but it is still not working.

                    Can you please advise where the script will need modifying?

                    thank you

                     

                    /* Add Signature Fields to Time Sheet

                    */ var ckWords; // word pair to test

                    numWords = this.getPageNumWords(0); // number of words on page

                    // loop through the words on page

                    for (var j = 0; j < numWords-1; j++) { // get word pair to test   

                    ckWords = this.getPageNthWord(0, j) + ' ' + this.getPageNthWord(0, j + 1); // test words   

                    // check to see if word pair is 'ENG APP' string is present   

                    if ( ckWords == "ENG APP") {     

                    var array1 =  this.getPageNthWordQuads(0, j);

                    var array2 =  this.getPageNthWordQuads(0, j + 1);

                     

                    // Convert quads in default user space to rotated

                    // User space used by Links

                    m = (new Matrix2D).fromRotated(this,0);

                    mInv = m.invert()

                     

                    r = mInv.transform(array1)

                    r=r.toString()

                    r = r.split(",");

                     

                    p = mInv.transform(array2)

                    p=p.toString()

                    p = p.split(",");

                     

                    var m = this.addField("EngSignature", "signature", 0,

                    [r[0]+p[0], r[1]+p[1], r[2]+p[2], r[5]+p[5]]);

                    break;

                    }

                    }

                    • 7. Re: Add signature field to second occurance of two words (Nth Word) on page
                      Bernd Alheit Adobe Community Professional & MVP

                      You must use the left coordinates from the first array and the right coordinates from the second array.

                      • 8. Re: Add signature field to second occurance of two words (Nth Word) on page
                        Jo_2013 Level 1

                        Thank you very much, by using the left coordinates from the first array and the right coordinates from the second array the digital signature appears on the two words. Now for the crunch!  I am looking to place the digital signature script on the second occurrence of the two words. A counter has been added to the script, which counts how many times the words appear on the page but the digital signature field is inserted on the first occurrence of the words. Can you please advise how to modify the if statement with the counter, so the second occurrence of the words is selected?

                        var ckWords; // word pair to test

                        numWords = this.getPageNumWords(0);

                        var count = 0;

                         

                        // loop through the words on page

                        for (var j = 0; j < numWords-1; j++) { / / get word pair to test

                        ckWords = this.getPageNthWord(0, j) + ' ' + this.getPageNthWord(0, j + 1); // test words

                        // check to see if word pair is 'ENG APP' string is present

                        if ( ckWords === "ENG APP") {

                        count ++;

                        if (count == 2)

                        {

                        var array1 =  this.getPageNthWordQuads(0, j);

                        var array2 =  this.getPageNthWordQuads(0, j + 1);

                        // Convert quads in default user space to rotated

                        // User space used by Links

                        m = (new Matrix2D).fromRotated(this,0);

                        mInv = m.invert()

                        r = mInv.transform(array1)

                        r=r.toString()

                        r = r.split(",");

                        p = mInv.transform(array2)

                        p=p.toString()

                        p = p.split(",");

                        var m = this.addField("EngSignature1", "signature", 0,

                        [r[0], p[1], p[2], r[5]]);

                        break;

                        }

                        }

                        }

                        • 9. Re: Add signature field to second occurance of two words (Nth Word) on page
                          try67 MVP & Adobe Community Professional

                          The code seems fine. It's possible that the internal order of the words is

                          not what you expect it to be, though.

                          1 person found this helpful
                          • 10. Re: Add signature field to second occurance of two words (Nth Word) on page
                            Bernd Alheit Adobe Community Professional & MVP

                            getPageNthWord will return the words in the order in the PDF file. This can differ from the order on the visible page.

                            1 person found this helpful