1 Reply Latest reply on Jul 26, 2015 3:24 PM by try67

    Acrobat:  Change certain words in a field green and certain words red

    brerick

      I am fairly new to JavaScript and brand new to scripting in Adobe.  I am an archaeologist and am trying to write a code that will examine a field in Acrobat and turn certain strings red (anytime the letter "L" appears followed by four numbers) and certain strings green (anytime the letter "W" appears followed by three numbers).  For example, I would want L1234 to turn red automatically and W123 to turn green automatically, while the rest of the text is left black.

       

      I have written the appropriate JavaScript to do this with HTML id's, but I am having difficulty translating the HTML /JavaScript script to run in Adobe Acrobat.  Is it possible to accomplish this through scripting in Adobe Acrobat?  I know how to turn text different colors within a field, but I have yet to figure out how to select specific words using conditional statement.  Thank you for your help.

       

      Link to code on codepen.io.

       

      Link to code on jsfiddle.

       

      Copy of Code:

       

      HTML

      <p id = "locus">L1223</p>

      <p id = "wall">W321</p>

       

      JavaScript

      /* Change locus number red */

       

      var locus = document.getElementById('locus');

      var locusString = document.getElementById('locus').innerHTML;

      var locusLength = locusString.length;

       

      if (locusLength == 5 && (locusString.indexOf('L') == 0 || locusString.indexOf('l') == 0) &&  (

          locusString.endsWith(1) ||

          locusString.endsWith(2) ||

          locusString.endsWith(3) ||

          locusString.endsWith(4) ||

          locusString.endsWith(5) ||

          locusString.endsWith(6) ||

          locusString.endsWith(7) ||

          locusString.endsWith(8) ||

          locusString.endsWith(9) ||

          locusString.endsWith(0)

          )

      )

      {

        locus.style.color ='red';

      };

       

      /* Change wall number gree */

       

      var wall = document.getElementById('wall');

      var wallString = document.getElementById('wall').innerHTML;

      var wallLength = wallString.length;

       

      if (wallLength == 4 && (wallString.indexOf('W') == 0 || wallString.indexOf('w') == 0) &&  (

          wallString.endsWith(1) ||

          wallString.endsWith(2) ||

          wallString.endsWith(3) ||

          wallString.endsWith(4) ||

          wallString.endsWith(5) ||

          wallString.endsWith(6) ||

          wallString.endsWith(7) ||

          wallString.endsWith(8) ||

          wallString.endsWith(9) ||

          wallString.endsWith(0)

          )

      )

      {

        wall.style.color ='green';

      };

       

      I realize there is likely an easier way to create this task in JavaScript, and as soon as I learn how, I will update my code.  Thank you for your help!

        • 1. Re: Acrobat:  Change certain words in a field green and certain words red
          try67 MVP & Adobe Community Professional

          You can't use JavaScript that was written for HTML files in Acrobat. The

          two environments are very different (although they both use the same core

          JS syntax).

          To have text in various colors in a PDF form field it has to be set as

          having Rich Text Contents and then you need to apply to it an array of Span

          objects, each one representing a part of the text that has the same

          properties. So each word that you want to "color" will need to be its own

          Span object, and the text between these words will also need to be such an

          object.

          This is possible to achieve, but it's not a simple scripting task.