Quantcast
Channel: Adobe Community : Popular Discussions - JavaScript
Viewing all articles
Browse latest Browse all 12130

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

$
0
0

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!


Viewing all articles
Browse latest Browse all 12130

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>