question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to render text layer

See original GitHub issue

I am trying to render the text content of a PDF as is done here. I wasn’t able to find an exposed API that would support this, event though #858 makes reference to page.renderTextLayer. Digging through the source I was able to find page.getTextContent and was able to get PDFJS.renderTextLayer by including src/display/text_layer.js in my source. Even then the text layer doesn’t render as expected.

Two questions:

  1. Why does the PDF text layer render incorrectly (example | source)?
  2. Why isn’t renderTextLayer exposed on either the page or PDFJS objects?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
pangguomingcommented, Feb 1, 2019

Hello,guys,i know how to do that. 1、incllude CSS file in your page <link rel="stylesheet" href="../../node_modules/pdfjs-dist/web/pdf_viewer.css"> 2、create a text layer div with the following code <div id="textlayer"></div> 3、replace js code: page.render(renderContext); to the following code

page.render(renderContext).then(() => {
          return page.getTextContent();
        }).then((textContent) => {
          // 创建文本图层div
          const textLayerDiv = document.getElementById('textlayer');
          textLayerDiv.setAttribute('class', 'textLayer');
          // 创建新的TextLayerBuilder实例
          var textLayer =  pdfjsLib.renderTextLayer({
            textContent: textContent,
            container: textLayerDiv,
            viewport: viewport
          });
          textLayer._render();
          //textLayer.setTextContent(textContent);
          //textLayer.render();
        });

5reactions
brendandahlcommented, Mar 9, 2016
  1. You need some more css. Checkout our main viewer for examples. But overall add a .textLayer class to your wrapping div and make it relative or absolute and
.textLayer > div { 
   position: absolute;
}
  1. Not positive what the above is asking, but it is exposed on PDFJS just not documented. We should probably move it to api.js and add some docs there.
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Enable Text Selection in PDF.JS - UsefulAngle
This method is renderTextLayer. As can be seen from the name, the method renders the text as a layer over the PDF. PDF....
Read more >
Generating the text layer - question regarding the textLayer ...
Parameter object supports: 'textLayer'. So I'm to assume I can do my render command like so: var renderContext = { canvasContext: ctx, textLayer:...
Read more >
pdf.js with text selection - javascript - Stack Overflow
On pdf.js version 2.8.61, the checked answer does no more work, as renderTextLayer() is integrated to pdf.js, no more outside source is ...
Read more >
Rendering Text In Just One Click - Planet Photoshop
If you need to convert your Type layer into an image layer, you can save some time by simply Control-clicking (PC: Right-clicking) directly ......
Read more >
Text layer - ngx-extended-pdf-viewer
By default, the text layer is switched off in order to render the document faster. The drawback is that both the find menu...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found