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:
When the UIView is opened, create a temporary file and set the PDF context to that file (UIGraphics.BeginPDFContext(filename)).
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.
Destroy the original PDF document created in step 2.
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.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With