Last active
October 27, 2017 13:35
-
-
Save yurydelendik/2a1879aa4f7575a3ca00 to your computer and use it in GitHub Desktop.
PDF.js get text layer divs bounds
This file contains hidden or 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 divBoundsFor(pageIndex) { | |
var page = document.querySelectorAll('.page')[pageIndex]; | |
var textLayer = page && page.querySelector('.textLayer'); | |
if (!textLayer) return null; | |
var textLayerBounds = textLayer.getBoundingClientRect(); | |
var divs = textLayer.querySelectorAll('div'); | |
var bounds = Array.prototype.map.call(divs, function (d) { | |
var r = d.getBoundingClientRect(); | |
return { | |
left: r.left - textLayerBounds.left, | |
top: r.top - textLayerBounds.top, | |
right: r.right - textLayerBounds.left, | |
bottom: r.bottom - textLayerBounds.top, | |
width: r.width, | |
height: r.height | |
}; | |
}); | |
return { | |
width: textLayerBounds.width, | |
height: textLayerBounds.height, | |
boxes: bounds | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello yurydelendik,
I have a problem with this code, when I try to
getBoundingClientRect()
for each div insidetextLayer
, they give 0 value for all attributes of their DOMRect.But it works perfectly with elements inside
annotationLayer
.I get this with Firefox 56.0 and Chromium 62.0.3202.62
I'm using pdfjs-dist 1.9.602