How to display a PDF document that includes hyperlinks? (on iOS)

女生的网名这么多〃 提交于 2019-12-21 23:24:03

问题


The Situation: I have a pdf document that includes hyperlinks. I would like my iPhone app to display this pdf document, and enable clicking of the hyperlinks.

Note: Currently, I am trying to do this with a UIWebView, which unfortunately seems unable to detect link taps. There is a family of classes & methods called CGPDF which I have read about in the documentation. The documentation talks all about parsing pdf documents and what not but is not specific on how to detect link taps and what not (thanks apple).


回答1:


I can't comment on UIWebView, but the CGPDF stuff is part of CoreGraphics. So it's primarily for drawing — you'll have a PDF object and a context to draw it to (almost certainly received via a custom subclass of UIView) and ask it to draw at a certain scale and position. If you're new to CoreGraphics and/or custom UIView subclasses then it might take a day or so to get into it, but it'll end up being a few hundred lines at worst.

CoreGraphics doesn't know anything about user interaction, so I think Apple considered it implicit that there's no concept of taps down there, and hence no way that CoreGraphics can directly help you with catching taps.

There's example code for loading a PDF and drawing it to a context at the top of this document. CoreGraphics is the same on OS X and iOS, except that the initial coordinate system is the opposite way up. On OS X it is the same way around as a PDF, on iOS it's the opposite way, so PDFs will render inverted along y if otherwise unadjusted. The section directly after the example display code is entitled 'Creating a Transform for a PDF Page' and should help with that — you just need to apply a transform that inverts y.

There's a tutorial here on how to create a custom UIView subclass that includes some CoreGraphics drawing steps.

With respect to catching taps, things get a bit more complicated. You don't need to do anything like a full PDF inspection because of the way the file format works. Things that are links are drawn to look however they should look per the normal PDF operations that CoreGraphics can handle for you. There are then separate tables that describe bounding box rectangles for catching taps and the links they should go to. Those tables are parsed into an easy to read format by CoreGraphics but not inspected. So you're given all the tools to find out where the links are without having to learn too much about the file format, but you still need to fish through for them yourself.

The relevant CoreGraphics construct is the CGPDFDictionary. You can grab the latest PDF specification here, but 99% of it isn't relevant to you or covers things that CoreGraphics already implements. You'll need to jump back and forth through the documentation to piece all the bits together, but you can leap in at section 8.4 on Annotations, on Page 488.

It ends up being a reasonably complicated combination of bits of code and I can't post my own for commercial reasons but you'll want to build up a mapping of page names to page numbers, traverse the link annotations (which are links to named pages) and do something to catch taps. Personally I just added invisible UIButtons as subviews of my custom PDF view. Obviously any transform you're applying to the PDF needs to be applied to the annotation coordinates too.

The total thing was maybe 1,000 lines.



来源:https://stackoverflow.com/questions/4117584/how-to-display-a-pdf-document-that-includes-hyperlinks-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!