Forked from yurydelendik/gist:f2b846dae7cb29c86d23
Last active
December 19, 2016 08:17
-
-
Save CGRemakes/c9c14b858829c4a4ac99 to your computer and use it in GitHub Desktop.
PDF.js get/show hightlight
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var id_count = 1; | |
function HighlightSelectedText() | |
{ | |
var pageIndex = window.PDFViewerApplication.pdfViewer.currentPageNumber - 1; | |
var page = window.PDFViewerApplication.pdfViewer.getPageView(pageIndex); | |
var pageElement = page.canvas.parentNode.nextSibling; | |
var pageRect = page.canvas.getClientRects()[0]; | |
var selection = window.getSelection(); | |
if(selection.rangeCount > 0) | |
{ | |
var selectionRects = selection.getRangeAt(0).getClientRects(); | |
var viewport = page.viewport; | |
var i = 0; | |
var selected = $.map(selectionRects, function(r){ | |
var rect = viewport.convertToPdfPoint(r.left - pageRect.left, r.top - pageRect.top).concat(viewport.convertToPdfPoint(r.right - pageRect.left, r.bottom - pageRect.top)); | |
var bounds = viewport.convertToViewportRectangle(rect); | |
if(!window.chrome || i % 2 == 0) | |
{ | |
var el = document.createElement('div'); | |
el.setAttribute('style', 'position: absolute;background-color:red;' + | |
'left:' + Math.min(bounds[0], bounds[2]) + 'px; top:' + Math.min(bounds[1], bounds[3]) + 'px;' + | |
'width:' + Math.abs(bounds[0] - bounds[2]) + 'px; height:' + Math.abs(bounds[1] - bounds[3]) + 'px;'); | |
el.className = 'hlight group_' + id_count; | |
pageElement.appendChild(el); | |
} | |
i++; | |
}); | |
id_count++; | |
window.getSelection().removeAllRanges(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment