annotationshighlightpspdfkit

How can I get the highlighted text from PSPDFKit?


I am using PSPDFKit to let users highlight text in a document. Is there anyway to get the actual text that was highlighted? So in the following example, I would like to get the text "the good and bad" when the user highlights it.

enter image description here

Looking at the PSAnnotations, I've tried the value and contents properties but neither gives me what I want.


Solution

  • I'm the CEO of PSPDFKit GmbH.

    All you need to do is get the PSPDFHighlightAnnotation (e.g. via annotationsForPage:type: in PSPDFDocument and then call highlightedString on it.

    Please be aware that this is only an approximation. We only know the position of each individual glyph on the page, and from that, PSPDFKit tries to reconstruct words, lines and text boxes. So highlightedString might not always return 100% what you'd expect, especially if the PDF is not well-designed. But in general, it's quite good.