如何显示包含超链接的PDF文档? (在iOS上) [英] How to display a PDF document that includes hyperlinks? (on iOS)

查看:432
本文介绍了如何显示包含超链接的PDF文档? (在iOS上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我有一个包含超链接的pdf文档。我希望我的iPhone应用程序显示此pdf文档,并启用单击超链接。

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.

注意:目前,我正在尝试使用UIWebView执行此操作,但遗憾的是,它似乎无法检测链接点击。有一系列课程和我在文档中已经读过的称为CGPDF的方法。该文档讲述了解析pdf文档的所有内容,以及没有具体关于如何检测链接点击的内容以及什么不是(感谢苹果)。

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).

推荐答案

我不能评论UIWebView,但CGPDF的东西是CoreGraphics的一部分。所以它主要用于绘图 - 你将有一个PDF对象和一个上下文来绘制它(几乎肯定是通过UIView的自定义子类接收)并要求它以一定的比例和位置绘制。如果您是CoreGraphics和/或自定义UIView子类的新手,那么可能需要一天左右的时间才能进入它,但它最终将最终成为几百行。

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对用户交互一无所知,所以我认为Apple认为隐含的是那里没有水龙头的概念,因此CoreGraphics无法直接帮助你捕捉水龙头。

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.

有一个示例代码,用于加载PDF并将其绘制到本文档。 CoreGraphics在OS X和iOS上是相同的,除了初始坐标系是相反的方式。在OS X上它与PDF相同,在iOS上则是相反的方式,因此如果不进行调整,PDF将沿y反转。示例显示代码后面的部分标题为为PDF页面创建转换并且应该有帮助 - 您只需要应用反转y的转换。

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.

这里有一个这里的教程创建一个包含一些CoreGraphics绘图步骤的自定义UIView子类。

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

关于捕捉水龙头,事情变得有点复杂。由于文件格式的工作方式,您无需执行完整的PDF检查。链接的东西被绘制成看起来它们应该按照CoreGraphics可以为您处理的普通PDF操作查看。然后有单独的表格来描述用于捕捉水龙头的边界框矩形以及它们应该去的链接。这些表由CoreGraphics解析为易于阅读的格式,但未经检查。因此,您可以使用所有工具找出链接的位置,而无需过多地了解文件格式,但您仍需要自己完成这些工作。

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.

相关的CoreGraphics构造是CGPDFDictionary。您可以在此处获取最新的PDF规范,但99%的它与您无关或涵盖CoreGraphics已实现的内容。你需要在文档中来回跳转以将所有位拼凑在一起,但是你可以在第488页的注释,第488页跳过。

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.

它最终是一个相当复杂的代码组合的组合,我不能出于商业原因发布我自己但你想要建立页面名称到页码的映射,遍历链接注释(这是指向页面的链接) )并采取措施抓住水龙头。就个人而言,我刚刚添加了不可见的UIButtons作为我的自定义PDF视图的子视图。显然,您应用于PDF的任何变换也需要应用于注释坐标。

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.

总计可能是1,000行。

The total thing was maybe 1,000 lines.

这篇关于如何显示包含超链接的PDF文档? (在iOS上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆