These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

How to Highlight text as if it has been selected or found?

asmohr
Registered: Sep 2 2007
Posts: 25

What I want to do is, from JavaScript, highlight some text on the page similarly to what happens when the user manually selects some text or when Search finds a hit.

I've been poring over the Developer Guide, the API Reference, and the forum postings on this site and the only things I've found that have come close have been the wrong way around.

I've seen many postings asking how to extract the text that the user has selected, but I want to highlight the text that I programmatically define.

I don't want to use a Highlight Annotation for two reasons. First, this is temporary, my implementation of the equivalent of a Search function that really is not "search" but operates in the same way, with current, previous, and next hits. An annotation is a permanent change to the contents of the file. So conceptually it seems the wrong way around. Second, related, is that I want to be able to do this in Reader, and Reader will not let you add Annots to a file that does not have that enabled, and I cannot count on all files having that set ahead of time. Some might be legacy files created in Acrobat 5 where the property does not exist (and defaults now to "off") and some might be owned by a third party and could not be modified and redistributed.

Does this capability exist directly, perhaps by specifying the quads or the Nth Words for the start and end of the text to highlight?

Perhaps I could do it indirectly, by using the Search highlight. Can Search be initiated from JavaScript and be restricted to a single page, so I could specify to find the Nth instance of a search-word on the page and have that be highlighted?

I have not found anything to suggest I could do either of these approaches.

Any ideas?

Thanks,
August

My Product Information:
Reader 9.0, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Take a look at these functions in the Acrobat JavaScript reference

doc.selectPageNthWord();
doc.getPageNthWord();

The Acrobat Search functionality can't be restricted to a single page.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

asmohr
Registered: Sep 2 2007
Posts: 25
Thanks, Thom,

That is getting really close. There's no way to highlight more than the one word, is there?

Also, to generate the code that selects the word, is there an easy way to identify the "N" for any manually selected word, or for the first word in a multi-word selection? I have not been able to find anything in the Reference that sounds like it could do the job.

Thanks,
August
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Unfortunately no. Word selection functionality is very weak in Acrobat JavaScript.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script