Skip to content

Instantly share code, notes, and snippets.

@yurydelendik
Last active October 27, 2017 13:35
Show Gist options
  • Save yurydelendik/2a1879aa4f7575a3ca00 to your computer and use it in GitHub Desktop.
Save yurydelendik/2a1879aa4f7575a3ca00 to your computer and use it in GitHub Desktop.
PDF.js get text layer divs bounds
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
};
}
@Vi-dot
Copy link

Vi-dot commented Oct 27, 2017

Hello yurydelendik,
I have a problem with this code, when I try to getBoundingClientRect() for each div inside textLayer, 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment