In PDF.js, how do you hide the canvas and display the underlying text at full opacity?

半城伤御伤魂 提交于 2019-12-08 03:33:30

Well, repeating your steps, I

  • removed .textLayer > div { color: transparent; },
  • added .pdfViewer .canvasWrapper { display: none; }
  • and lastly changed the opacity of the text layer .textLayer { opacity: 1.0; }.

The last one did the trick.

To do this programmatically via JS, you could use:

var mainCSS = document.styleSheets[0];
mainCSS.insertRule(".textLayer { opacity: 1.0; }", 1);
mainCSS.insertRule(".textLayer > div { color: initial !important; }", 1);
mainCSS.insertRule(".pdfViewer .canvasWrapper { display: none; }", 1);

The !important after color: initial is used to prevent the original CSS definition (color: transparent) from being applied.

Edit:

To prevent that text is drawn to the canvas, you could disable the functions that are used to draw text (namely fillText and strokeText).

CanvasRenderingContext2D.prototype.strokeText = function () { };
CanvasRenderingContext2D.prototype.fillText = function () { };

That way you will not have to modify the code in PDF.js itself.

If you want to preserve the functionality of strokeText and fillText you might be willing to adjust the functions showText and paintChar (within pdf.js / pdf.worker.js).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!