So you successfully added an annotation feature in your PDF.JS web application. You created some shapes onto the Canvas and you want to get the text that are within the bounds of the annotation shape.

How can you do it?

Take this image for example. See the yellow highlighted shape?

pdfjs_annotation1

 

The only solution to do this is to add a Span tag for each character because doing this you will be able to get the offset x,y coordinates as well as the width and height and use these details to see if it intersects within the annotation shape’s bound.

This is my custom made function:

By passing the annotation into the parameter, it will get its x, y, width and height details and loops through all the text Div layer of PDF.JS in the page number where the annotation is placed.

Then, if the text Div layer’s bound intersects that of the annotation shape’s bound, it will loop through all the text and add a Span tag.

Each span tag will have its own x, y, width and height and it will be used to see if it intersects with the annotation shape’s bound. If so, the character will be added to the buffer variable.

The buffer variable is the one that stores all the characters found to be within the bounds of the annotation shape.

The annotation parameter should be some annotation class that has a pageIndex and x, y, width and height attributes.

Related Posts Plugin for WordPress, Blogger...