I'm working around with pdfjs library and facing this problem when rendering the textlayer. The text in the textlayer isn't the same with the content in the canvas. How can I solve this? I saw there is textDiv and textDivProperties in the textlayer render parameters, are they playing some role in this case?
this is my code
// elements code
const canvasElement = document.createElement("canvas")
const textLayer = document.createElement("div");
textLayer.id = `textlayer-${index}`;
textLayer.style.position = "absolute";
const canvasWrapper = document.createElement("div")
canvasWrapper.style.width = "800px"
canvasWrapper.appendChild(canvasElement)
canvasWrapper.appendChild(textLayer)
canvasWrapper.id = `page-${index}`
canvasWrapper.style.marginTop = "2px";
canvasWrapper.style.marginBottom = "2px";
container.appendChild(canvasWrapper);
// render code
const renderContext = {
canvasContext: canvas.getContext('2d'),
viewport: viewport,
annotationMode: AnnotationMode.ENABLE_FORMS,
annotationCanvasMap: annotationCanvasMap
};
page.render(renderContext as RenderParameters).promise.then(() => {
return page.getTextContent({includeMarkedContent: true, disableNormalization: true});
}).then((textContent) => {
const textLayer = document.getElementById(`textlayer-${index}`);
if(textLayer == null) return;
textLayer.style.left = canvas.offsetLeft + 'px';
textLayer.style.top = canvas.offsetTop + 'px';
textLayer.style.height = canvas.offsetHeight + 'px';
textLayer.style.width = canvas.offsetWidth + 'px';
pdfjs.renderTextLayer({
textContentSource: textContent,
textContent: textContent,
container: textLayer!,
viewport: viewport,
textDivs: []
} as TextLayerRenderParameters);
});
if(isGettingURL == true) {
updateIsGetting(false);
}
Had encountered this recently, turns out you only need to give your textlayer element a class name
.textLayer
then importpdf_viewer.css
frompdfjs-dist/web/
Import CSS (I used SCSS here)
Then add classname
Here's an extract of the styles used:
Note that the pdf_viewer.css also contains styles for annotation and xfa Layers