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

Highlight Text in PDF using JSObject

venkatesh_ur
Registered: Jun 11 2008
Posts: 5

I am doing a PDF compare application using VB.NET and JSObject. I did all the compare logic and extracted all the mismatching words and their X-Y co-ordinates. Now its the time to highlight all the acquired co-ord with some color. I get 8 values when I get access the co-ord using the below fn.

oCoord = JSO.getPageNthWordQuads(Page, Word)

I tried up the following with simple X-Y co-ordinates it works great. But when I give using the co-ordinates what I get from the exact location (i.e., I get 8 values which describe a rectangle), I get the following error,

"Value does not fall within the expected range."

Here is the failed code -

page = pdDoc.AcquirePage(0)

'' I get pageRect.Y = 842 and pageRect.X = 595
pageRect = page.GetSize

popupRect(0) = 36D
popupRect(1) = 673.171997070312D
popupRect(2) = 96.69580078125D
popupRect(3) = 673.171997070312D
popupRect(4) = 36D
popupRect(5) = 659.300109863281D
popupRect(6) = 96.69580078125D
popupRect(7) = 659.300109863281D

annot = jso.AddAnnot
props = annot.getProps
props.Type = "Highlight"
annot.setProps(props)
props = annot.getProps
props.page = 0
props.popupRect = popupRect
props.strokeColor = jso.Color.Yellow
annot.setProps(props)

What is that I am missing? Or do I need to do some manipulation with the co-ordinates.

Here is the code which works fine -

page = pdDoc.AcquirePage(0)

'' I get pageRect.Y = 842 and pageRect.X = 595
pageRect = page.GetSize

popupRect(0) = 0
popupRect(1) = pageRect.y
popupRect(2) = 200
popupRect(3) = pageRect.y

annot = jso.AddAnnot
props = annot.getProps
props.Type = "Highlight"
annot.setProps(props)
props = annot.getProps
props.page = 0
props.popupRect = popupRect
props.strokeColor = jso.Color.Yellow
annot.setProps(props)

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I don't see any where in the code where the annot's "quads" property is set. This is mandatory for the Highlight Annotation. The popupRect has nothing to do whith placing the highlight, it's for placing the popup text box associated with the highlight.

Is the second set of code really working as far as placing the highlight? or is it just not throwing an error?

There is an example of createing a "Text Markup" annotation in the Acrobat JavaScript Reference, Look up the "getPageNthWordQuads" function. Also read about the annot.quads property. It's a list of points that from a rectangle around the text. A simple rectangle cannot be used because the text might be rotated.

Here is an artical that explains about page coords and adding annotations to a PDF.

http://www.acrobatusers.com/tutorials/2007/10/auto_placement_annotations/

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

venkatesh_ur
Registered: Jun 11 2008
Posts: 5
Thanks for your response.

Ya, I had modified the code a bit to get the rectangle object for highlighting the desired text.

When highlighting a single word, it works great. But when multiple words are highlighted, it is getting messed up. (The entire text content is getting highlighted)

The sample is available in the following link -
http://www.adobeforums.com/webx/?128 [at] 27 [dot] Bunvic[email protected] have no clue whether any other setting need to be changed to highlight multiple words in a single shot.

Please help me out.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
The highlight annotation works by accepting a list of Quads. Each quad is a separate highlight on the page, but they are all part of the same annotation. If you want to highlight several words then you have to collect all the Arrays of Quads for those words and append them together to make a master list of quads.

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

venkatesh_ur
Registered: Jun 11 2008
Posts: 5
The logic what u said is the one I had implemented except the one I made use of ArrayList to hold the quads.

But it is getting messed up. Do u have any code samples which works perfectly for you?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I have a philosophy about using JS from VB. If the code is large, or involves anything more than the simplest data types, then I write a folder level JS function to do all the heavy lifting. Then call the function from the VB. This gets around all sorts of problems. I'd suggest you do this. You won't have any of these same problems if you're coding in the Acrobat JS environment.

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

erieru
Registered: May 18 2011
Posts: 1
Hi.

I am programming the same thing using JSObject.
When I set the quads using the following code,
Run-time error "Automation error.Invalid access to memory location" occurs.

----------------------------------------------
props.quads = jso.getPageNthWordQuads(page, i)
----------------------------------------------

Can someone tell me how to set quads?
And how to a folder level JS function from the VB?


Best regard.