How to select lines of text in a PDF and then highlight them? (iOS)

annotations, ios, pdf, quartz-graphics

Solution

Your potential solution is the way to go. The size of the bounding rectangle of a Tj string is the sum of bounding rectangles of each glyph in the string so you can select anything in the string. THe PDFKitten library might help you with text processing: https://github.com/KurtCode/PDFKitten

Problem

I have searched for PDF annotation on Google, stackoverflow, and Apple's developer library but none of them really satisfies my need. Here is my problem and my current knowledge of it, please correct me if I'm wrong. I would like to allow users to first select some lines of text to highlight, and then draw highlighting rectangles on top them. [Question]: How do we select text on a PDF page rendered by CGContextDrawPDFPage()? Specifically, if I were to override the touchesBegan/Moved/Ended delegate methods, what should I do in there in order to track the text frame that a user wants to select? I don't want to use FastPDFKit because it displays a logo and costs money, or UIWebView because it is very limited in terms of things that we can do with it, plus I would like to learn how to do this myself :-) Thanks! Derek -- UPDATE -- Potential solution: I could probably put a transparent view on top of my PDF, and while parsing the PDF content, build a rectangle for every Tj or every string element in TJ. Those rectangles would be the selectable "text" that users can highlight. One caveat is that some PDFs jam several words into one Tj, so that prevents users from selecting individual words in that case. I will try this approach and update the result later.

Original source