-
-
Save nguyenbathanh/931e84f06c967375cef00ba6ee37074f 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
function getHightlightCoords() { | |
var pageIndex = PDFViewerApplication.pdfViewer.currentPageNumber - 1; | |
var page = PDFViewerApplication.pdfViewer.getPageView(pageIndex); | |
var pageRect = page.canvas.getClientRects()[0]; | |
var selectionRects = window.getSelection().getRangeAt(0).getClientRects(); | |
var viewport = page.viewport; | |
var selected = selectionRects.map(function (r) { | |
return viewport.convertToPdfPoint(r.left - pageRect.x, r.top - pageRect.y).concat( | |
viewport.convertToPdfPoint(r.right - pageRect.x, r.bottom - pageRect.y)); | |
}); | |
return {page: pageIndex, coords: selected}; | |
} | |
function showHighlight(selected) { | |
var pageIndex = selected.page; | |
var page = PDFViewerApplication.pdfViewer.getPageView(pageIndex); | |
var pageElement = page.canvas.parentElement; | |
var viewport = page.viewport; | |
selected.coords.forEach(function (rect) { | |
var bounds = viewport.convertToViewportRectangle(rect); | |
var el = document.createElement('div'); | |
el.setAttribute('style', 'position: absolute; background-color: pink;' + | |
'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;'); | |
pageElement.appendChild(el); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment