How can I cleanly draw on a PDF both statically and dynamically on iOS?

515 views Asked by At

I am currently working on an app where we would like to download a PDF from a remote server and then draw on it. We would like to draw Google Maps pin-like annotations on the PDF (the static draw part). Furthermore, we would like to detect if a user has touched a pin and then draw a calloutBox over this PDF (dynamic draw part). We obviously would like the pdf to be scrollable/zoomable. Does anyone know of a good way to achieve this?

Things I have researched: 1) Render in a UIWebView. This seems like a great solution but its not clear to me how to then implement the draw code on the PDF. I have heard people say create a transparent UIView above the UIWebView for the drawing. This seems to come with its issues, how will it handle zooming and scrolling?

2) Use Quartz 2D and generate my own PDF from the PDF I fetch from the server. As I draw my own PDF content I can draw the static marker pins. Once I have this PDF, I can then shove it in a WebView. The problem with this approach however is I still need to handle the dynamic drawing of the call-out boxes when a user taps on the pin and this then kinda takes me back to problem 1.

1

There are 1 answers

1
steipete On BEST ANSWER

You're correct that Apple does not offer much in terms of this issue. There's UIWebView which can preview and show PDF documents, but it's really not suited to adding annotations, and any "solution" with views will be very fragile, if you manage to do it at all. It's meant as a black box to read PDF documents, not for annotating.

You have to go all the way back to CGContextRef and take over the scrolling, zooming and touch handling/drawing yourself. Apple's ZoomingPDFViewer example is a good start.

I have been working on this problem since 2010 and we offer a commercial solution for PDF annotating for iOS, Android and Web called PSPDFKit. We ship a custom renderer which is better and more exact than Apple's CoreGraphics renderer, but the more interesting part is that we can deal with all common PDF annotation types. You can use note annotations to represent your pins and move them around, add notes, interact/override the default tap handling (and e.g. show your own popover when people tap on them). They are also always the same size - so they can be anchored at an exact point in the PDF and then you can zoom in while they stay the same size. The best part is that this is all part of the PDF spec, so they will also work with Apple's Preview app or Adobe Acrobat, so people can save/customize the markup and then everything can be saved in the PDF. The architecture is flexible so you can also simply save everything in a database or sync it back up to your server and simply use it for touch handling.

You can also build that yourself - the basic architecture is a UIScrollView and views that are managed. It quickly gets tricky when you do zooming and have views that need to stay the same size + touch handling and maybe you also want things like multi-select or regular ink drawing. You will also want to add some sort of image caching layer, since rendering PDF documents can be quite slow on mobile devices. Oh, and if you want to make text selectable or implement search, be ready for a rabbit hole that is called the Adobe CMap and CIDFont Files Specification.