Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 684
Derek Sun Avatar asked May 03 '12 01:05

Derek Sun


People also ask

Can you highlight a PDF on Iphone?

Tap on the PDF to bring up controls and then tap on the Markup icon. This gives you all the same options with highlight colors and the ability to draw with your finger or Apple Pencil.

Can I highlight a line in a PDF?

Click and drag the cursor to select the area of text you want to highlight. A small toolbox will appear above your text. Select the highlighter tool icon and it will automatically highlight your selected text. To modify, click on the highlighted area, and a new toolbox will appear.

How do you highlight in a PDF on Apple?

In the Preview app on your Mac, open the PDF you want to change. Do any of the following: Quickly highlight, underline, or strike through text: Select text, click the down arrow next to the Highlight button , then choose a highlight color, underline, or strikethrough.


1 Answers

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

like image 148
iPDFdev Avatar answered Sep 29 '22 08:09

iPDFdev