Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw into PDF on iOS?

I am trying to find a way to allow the user to annotate (i.e. free form drawing) an existing PDF file in iOS, but the only approach that I have found seems extraordinarily cumbersome.

The solution is in Monotouch, but I can translate from Objective-C if needed. This will also mean that many of the method and property names may not be completely familiar to non-Monotouch developers, but I am just looking for an approach.

I currently have code that allows users to annotate images (jpg, png) using the TouchesMoved and TouchesBegan events on a UIView. This works well and accomplishes what I need.

However, when working with an existing PDF file, it seems that the majority of the CGPDF functionality is centered around creating new files rather than editing existing ones.

The only approach that I have been able to come up with is the following:

  1. When the UIView is opened, create a temporary file and set the PDF context to that file (UIGraphics.BeginPDFContext(filename)).

  2. Create a new CGPDFDocument from the original file and then draw each page from the original PDF into the current context, which will be the new temporary file.

  3. Destroy the original PDF document created in step 2.

  4. Show the user the first page and then let them draw on it, with the drawing commands being sent to the new file's context. There will also be methods to allow the user to change pages.

  5. When the user is done, close the PDF context, which will save all of the annotations to the new PDF file.

This seems like it has the potential to be an enormous memory and performance hog, so I am wondering if there is any other way to approach this?

like image 917
competent_tech Avatar asked Mar 30 '12 02:03

competent_tech


1 Answers

The way that we finally accomplished this was to draw the PDF into a regular view, then insert a subview above this view where the annotations are actually drawn. We have to record the annotations as they occur and adjust for zooming. When the user is done, we draw each page of the PDF into a new PDF file and play back the annotations into that page, thus effectively adding annotations to an existing PDF.

like image 120
competent_tech Avatar answered Sep 27 '22 19:09

competent_tech