Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Javascript to search within a pdf for specific text

4,278 views
Skip to first unread message

Gordon Goldsmith

unread,
May 22, 2003, 4:06:40 PM5/22/03
to
How can I use javascript to programtically search the text within a pdf document for a list of names and highlight the text so that it is easy for a user to spot the names in the list.

I MS Word this is easy to do,, I have searched and cannot find an easy way to do this or anyway to do this.

I thought maybe the app.execMenuItem might work but I don't know how to pass it to call the find function and pass it the value to search the text for and then highlight the selected text, for each name in the list.

Any ideas?

Thanks,

Aandi Inston

unread,
May 22, 2003, 5:31:31 PM5/22/03
to
You have to step through each page, and then step through the words on
each page (Get...NthWord). I guess you might be able to highlight,
not sure about that. Each word has a "quad" that identifies its
location on the page.

Aandi Inston

Tobias Hugener

unread,
May 23, 2003, 8:45:51 AM5/23/03
to
Also, where can I get details on the execMenuItem and the options that are available for it.

In v5, use

console.clear();
console.show();
app.listMenuItems();

to get all menu items with JavaScript (you can't pass any parameters).

I'm afraid I can't help you with the other questions.

Tobias

Gordon Goldsmith

unread,
May 23, 2003, 8:37:23 AM5/23/03
to
Aandi,
Thanks, how to I determine the number of pages and words for the loops? Does Acrobat support DOM?

Any ideas on how to perform the highlight? In Acrobat 5 there is a highlight tool but you click on that and then select the text to highlight. I don't know how you would "select" text then apply the highlight to it unless it was a property of the text object that contained the text that was selected. I can't find anything on creating an object to contain the text or selection either.

Also, where can I get details on the execMenuItem and the options that are available for it.

I haven't worked with Acrobat in a long long time.

Thanks for all the help.

dsw

unread,
May 23, 2003, 9:26:25 AM5/23/03
to
Gordon,

To determine the number of pages in the document, use this.numPages;

To determine the number of words on a given page, use the getPageNumWords() method.

You can find information about both of these functions in the Acrobat JavaScript guide. It might even contain information about the other things you're looking to do.

Gerry Heinemann

unread,
May 23, 2003, 11:53:51 AM5/23/03
to
The Annot Object allows you to highlight text.
Javascript 5.1 Guide Page 52.
Gerry

dsw

unread,
May 23, 2003, 12:29:42 PM5/23/03
to
One problem that you may find with using Annot to highlight is that the document will actually be modified. If the user saves the document (and the program will prompt for this when the document is closed) the highlighted words will remain highlighted.

Andrew E D Clark

unread,
May 24, 2003, 7:12:45 AM5/24/03
to
I presume this is a situation where using 'Find' will simply not do what is required. Is this a defined list of names, or can the user choose what they want to search for?

cvu

unread,
Jun 17, 2003, 1:15:58 AM6/17/03
to
Here is the sample js code to create highlight annot:

var thisPage = this.pageNum;
var numWords = this.getPageNumWords(thisPage);
for ( var j = 0; j < numWords; j++) {
nthWord = this.getPageNthWord(thisPage,j)
if ( nthWord == "mark" ) {
aQuadsFirst = this.getPageNthWordQuads(thisPage,j);
aQuadsLast = this.getPageNthWordQuads(thisPage,j+2);
annot = this.addAnnot({
page: thisPage,
type: "Highlight", // "Underline", "StrikeOut", or "Squiggly"
strokeColor: color.yellow,
quads: [[ aQuadsFirst[0][0], aQuadsFirst[0][1],
aQuadsLast[0][2], aQuadsLast[0][3],
aQuadsFirst[0][4], aQuadsFirst[0][5],
aQuadsLast[0][6], aQuadsLast[0][7]
]],
author: "A. C. Acrobat",
contents: "Highlight, Underline,\rStrikeOut, and Squiggly"
});
break;
}
}

0 new messages