How to get selected text under an annotation

91 views
Skip to first unread message

Matt Bojey

unread,
Jan 27, 2016, 12:44:45 PM1/27/16
to pdfnet-w...@googlegroups.com
Q: How can I get text under an annotation?

A:  Here is some code that should work.  It's adding .highlight at the end of the textSelected event name so that we can remove only this particular handler for the textSelected event.

function getSelectedText(annotation, callback) { 
    docViewer.on('textSelected.highlight', function(e, quads, text) {
        docViewer.off('textSelected.highlight');
        docViewer.clearSelection();
        callback(text);
    });

    var numQuads = annotation.Quads.length;
    var firstQuad = annotation.Quads[0].getPoints();
    var lastQuad = annotation.Quads[numQuads - 1].getPoints();

    var topLeft = {
        x: firstQuad.x1,
        y: firstQuad.y1,
        pageIndex: annotation.getPageNumber() - 1
    };
    var bottomRight = {
        x: lastQuad.x3,
        y: lastQuad.y3,
        pageIndex: annotation.getPageNumber() - 1
    };
    var textSelectTool = new Tools.TextSelectTool(docViewer);
    textSelectTool.select(topLeft, bottomRight);
}
Reply all
Reply to author
Forward
0 new messages