Re: adding PDF annotations to the output of a regular NSView
Re: adding PDF annotations to the output of a regular NSView
- Subject: Re: adding PDF annotations to the output of a regular NSView
- From: John Calhoun <email@hidden>
- Date: Wed, 29 Jul 2009 17:56:54 -0700
On Jul 29, 2009, at 4:44 PM, Martin Wierschin wrote:
So the problem is that there doesn't seem to be any way of marrying
these two systems together. Ideally there would be some Cocoa method
I could call in my NSView's drawRect method to add PDF annotations.
But the only solution I see is something like this:
1. Produce raw PDF data from my normal NSView hierarchy.
2. Load that NSView data into a PDFView.
3. Add PDFAnnotations to special regions as desired.
4. Resave the modified PDF data from the PDFView.
I'll start by pointing out that Step 2 is not necessary. Adding
annotations in PDF Kit is done at the PDFDocument level. The
PDFDocument you will have created from your raw PDF data (from Step
1). The PDFView is strictly for display purposes.
As well then Step 4 more accurately would be "re-save the modified
PDFDocument as PDF data."
This seems very inefficient and error prone (eg: mapping coordinates
between NSView and PDF page space). Is this really the best option
available?
Mapping points from NSView to page space is not an issue. Since the
PDF data came from your NSView to begin with you can use the same
coordinates you used in the NSView. So for example if the special
region was of some bounds (NSRect) in the NSView you can use this same
rect to create your PDFAnnotation and add it in PDF Kit.
But if you wanted a more straightforward model (and I understand that
NSView->PDF data->PDFDocument is a bit of a kludge) you could replace
your NSView with a subclass of PDFPage and display with a PDFView. It
depends on your application though ... how much of a kludge this
approach would be (you could simply be trading one kludge for
another :-)).
But in essence to do this a PDFView replaces the NSView and rather
than overriding -[drawRect] as you might for NSView, you create a
PDFPage subclass of your own and override the page's -[drawWithBox:]
method as the place to draw your own content. You add your page to an
empty PDFDocument and display this document in the PDFView.
To be clear above, the PDFPage is not a view classes but does have a
draw method that is called by PDFView to display the page content. For
a PDF you find laying around, the default implementation of PDFPage is
to call upon the PDF data to render the specified page. Your subclass
allows you to draw whatever you like.
The advantage to the above approach is that you can in fact add the
PDFAnnotation prior to saving and have it actually function within
your application (since PDFView will handle the mouseDown in the
annotation). And then as well you get zoom controls and all that with
the PDFView class....
But as I say, it sort of depends on your app. If your app is an
appoinment calendar then writing it to live within a PDFView is a bit
of a stretch. :-)
John Calhoun—
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden