Copy link to clipboard
Copied
Hello, I've search to no avail. I want to highlight certain text lines on a pdf form if the user selects a particular option from a dropdown list on the form. The text I want to highlight is not text fields but just regular text in the document. Thanks!
Copy link to clipboard
Copied
The easy way to do this is to place the highlight over the text, set it to be hidden, and then use a script on the dropdown to turn the visibility on and off. So there is a bit of a process to this.
Here's the setup:
1)Place Highlight Annot
2) Select Annot
3) Open Console Window
Find a video tutorial on the Console here: https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro
4) Run this code in the console to set the name of the highlight, and make it hidden
selectedAnnots[0].hidden = false
selectedAnnots[0].name = "HG1"
Here the name is "HG1", but you could make it anything.
And there's the setup. Use this code to make the highlight visibile
this.getAnnot(page#, "HG1").hidden = true;
where page# is the actual page number.
So the next step is to add this code to a dropdown script.
You'll find what you need in the article here:
https://acrobatusers.com/tutorials/change_another_field
You can find much more info on list and dropdown fields here:
https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm
Copy link to clipboard
Copied
Thom, I watched the tutorial on the Console and I'm not sure how to "run the code in the console" or what you mean by make it hidden then make it visible. ?
Copy link to clipboard
Copied
I'm getting this in the Console when I enter the code you provided:
selectedAnnots[0].hidden = false selectedAnnots[0].name = "HG1" HG1 this.getAnnot(page#, "HG1").hidden = true; SyntaxError: illegal character 1:Console:Exec undefined
Copy link to clipboard
Copied
One more note: be aware that this level of scripting will only work on PDF viewers that support markup annotation scripting. Basically Acrobat Pro/Reader and a handfull of desktop viewers.
Copy link to clipboard
Copied
I have Acrobat XI 11.0.23
Does this work? I can access the console but not sure how to assign the script to the highlighted annotation
Copy link to clipboard
Copied
Yes this will work on your version of Acrobat.
I think you might be mixing some things up. Let's take it one bit at a time.
First, This is the only code you need to run in the Console window. And it's two separate lines.
selectedAnnots[0].hidden = true;
selectedAnnots[0].name = "HG1";
Run each one separately. The "selectedAnnots" property is a list of annotations that are currently selected. So be sure the highlight is selected.
the first line hides the highlight. You can make it visible again by running this code in the console
selectedAnnots[0].hidden = false;
When you can make this work you are ready for the next phase.
Copy link to clipboard
Copied
Thanks for your patience. Please bear with me on this. This is what I see when I run the 2 lines for code in the console window but the highlighted annotation doesn't disappear. Im pressing Command + Enter on Mac. Thx
Copy link to clipboard
Copied
I was able to make this highlighted annotation disappear. Now I'm not sure how to control its ON/OFF by what's selected from a dropdown.
Copy link to clipboard
Copied
Use this code as the custom validation script of your drop-down:
var annot = this.getAnnot("HG1", X);
if (annot) annot.hidden = (event.value!="Some value");
You need to edit two things in it:
- Replace X in the first line with the page number where the annotation is located (minus one, as pages in JS are zero-based, so 0 is the first page, 1 is the second page, etc.)
- Replace "Some value" in the second line with the actual value you want to use when the highlight should be visible.
Also, make sure you tick the option to commit the value of the drop-down field immediately, under the Options tab of its Properties dialog.
Copy link to clipboard
Copied
Thank you. Can you please expand on the part about replacing the "some value" with an actual value? I just want to turn the highlights on when a particular dropdown option is selected. Thx!
Copy link to clipboard
Copied
some value = particular dropdown option
Copy link to clipboard
Copied
try67 and Thom,
Here's some screenshots of my console inputs, dropdown custom validation inputs, and then here's the console error I got when I tried to select "Model Maintenance" from the dropdown? Any advice? I feel like I'm real close to getting this and really appreciate the help. Here's the error from console:
GeneralError: Operation failed. XMLData.prototype:0: Missing Mesh Restriction
Copy link to clipboard
Copied
That's a strange error message. Could you share the actual file with us?
Copy link to clipboard
Copied
A "mesh restriction" is an error associated with the XML scripting model. It appears to be confusing your code with an XML object. Ignore the message. It just means something is wrong.
It looks like this is your validation code
if (annot) annot.hidden = (event.value = "Model Maintenance");
It should be
if (annot) annot.hidden = (event.value == "Model Maintenance");
However, the code error should not be causing an error. Can you post the code you are using so we can read it.
Copy link to clipboard
Copied
Thom and try67,
Here's the file link. I replaced the validation code with what you recommended and it looks like I'm still getting the error and nothing highights. I'm trying to get selected lines in the "Description" column to become highlighted when I select "Model Maintenance" from the dropdown menu. Thank you.
http://www.tensascamps.com/uploads/6/9/3/0/6930281/nestwell_properties_maintenance_checklist2.pdf
Copy link to clipboard
Copied
You didn't create any comments...
Copy link to clipboard
Copied
Can you please clarify what you mean by "Comments"? Maybe that's where I'm going wrong. I did highlight a line of text which showed up as a comment but it became hidden when I ran this code in the Console:
var annot = this.getAnnot("HG1", X);
if (annot) annot.hidden = (event.value!="Some value");
Copy link to clipboard
Copied
There is an error in your code. Details are very important for any kind of programming. You need to read and follow instructions carefully. Things need to be exact. That's why there are references for each object, function, and property. Here is the Acrobat JavaScript reference entry for the "doc.getAnnot()" function.
You'll see right away what you did wrong.
Otherwise, the document is fine.
Copy link to clipboard
Copied
I used this code in the custom validation script box and it worked!
var ann = this.getAnnot(0, "HG1");
if (ann) ann.hidden = (event.value!="Model Maintenance");
It performs the funtion on Acrobat Pro desktop, but it does not seem to work on iPad and iPhone apps for PDF Expert and Acrobat Reader. I know these apps support some javascripting. Are y'all aware of an alternate method to write the code or alternate app to read the code to perform this funtion? Thx!
Copy link to clipboard
Copied
If it doesn't work with PDF Expert it's not likely to work at all.
The only alternative is the getAnnots method, which retrieves multiple annotations, not just one.
If that one works then you could scan the returned annotations array looking for the specific one you want to show/hide.
Copy link to clipboard
Copied
The Adobe and Readdle mobile PDF viewers do not implement the complete JavaScript model. Mostly they just cover basic form scripting to support calculations. The Readdle PDF Expert is much better than the Adobe Mobile Reader, but even for that one I don't think there is anything to support annotations, so unfortunately this level of PDF interactivity is no going to work on mobile.
Copy link to clipboard
Copied
Sorry, my bad. The comment does exist but as Thom correctly pointed out you're not using the getAnnot method correctly to retrieve it.
Copy link to clipboard
Copied
Hi Thom,
How can I use the code above to highlight multiple text fields for the same dropdown selections. I have it functioning on one but cannot get it to work for multiple.
Copy link to clipboard
Copied
Just duplicate the code, adjusting the name of the comment (and page number, if necessary) each time:
var ann = this.getAnnot(0, "HG1");
if (ann) ann.hidden = (event.value!="Model Maintenance");
var ann = this.getAnnot(0, "HG2");
if (ann) ann.hidden = (event.value!="Model Maintenance");
var ann = this.getAnnot(0, "HG3");
if (ann) ann.hidden = (event.value!="Model Maintenance");
etc.