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.

User Tim Down from the Stack Overflow forum provided a very handy function to get the rectangle bounds of selected text in Javascript.

This is mighty useful in cases like mine where I wanted to control the visibility of the context menu and that I required it to appear only when the mouse cursor is within the selected text bound.

The problem in Java FX when we hide controls is that once we set it to setVisible(false), the control will be hidden yes. But its bounded space will still be in effect and it would look pretty ugly seeing a huge empty area in your user interface.

While some have suggested placing it in an HBox or VBox and removing and adding them when needed, the better solution for me is to call this control.managedProperty().bind(control.visibleProperty()); before any calls to setVisible() whether you want to hide it or not.

Once that is called, when you hide a control, its bounded space will be removed from the user interface as well making it look like it is not there at all.

Related Posts Plugin for WordPress, Blogger...