I'm new in iphone I opened a pdf file using CGPDFDocument. I want to make it when do a long press in a specific line in document, do a highlight. any sample code to do this??

Thanks in advance.

有帮助吗?

解决方案

You need to roll your own solution. Apple added text selection as private API in iOS, but they haven't exposed anything. You can simply use UIWebView to get that feature, or, if you need more control or also want features like highlighting, you need to write your own solution.

For general PDF readers, there's quite some open source code out there. (see vfr/Reader as an example)

To also get text selection/annotations, it's a whole different level of pain. I worked full-time the better part of the year on those features, and just released v2 of my commercial framework PSPDFKit.

There are two problems you would need to solve when rolling your own; first getting the glyph rects and finding words from the single glyphs. I used CoreGraphics as a parser - calculating the correct frame and managing the current drawing stack and different font styles is the hard part. There's some open source code out there (PDFKitten) but I finally rolled my own because that one really isn't great and has many problems with various PDF font formats.

Second, writing actual annotation objects. Here you need to parse the PDF yourself to make a tree of all the objects, then write a new trailer that replaces the /Pages object (PDF is mostly just text, so it's well-parsable; but still quite hard to get right). I'm not aware of any open source code out there that could help you with that. I spent a lot of long nights with the official PDF reference.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top