I have developed a pdf viewer with all your suggestions and code snippets. Thanks :) Now i want to make it a pdf editor. I want to create an app for iphone/ipad similar to P
So in the title you say that you want to add an annotation to a pdf, but then the example that you are trying to make work in the body of your question is simply adding text to the pdf. These are very different things....
Here is a "Hello World" which adds text to an existing pdf (similar to your attempt):
I have not tested this but it is based on existing code (where I was drawing using a CTLine instead of CGContextShowTextAtPoint) so it should be very close. Error checking has been removed for clarity.
/*
* This function copies the first page of a source pdf into the destination pdf
* and then adds a line of text to the destination pdf.
*
* This must be modified by putting the correct path into the NSURL lines
* for sourceURL and destURL before it will work.
*
* This code is using ARC and has error checking removed for clarity.
*/
- (void)helloWorldPDF {
// Open the source pdf
NSURL *sourceURL = [NSURL fileURLWithPath:@"path to original pdf"];
CGPDFDocumentRef sourceDoc = CGPDFDocumentCreateWithURL((__bridge CFURLRef)sourceURL);
// Create the new destination pdf & set the font
NSURL *destURL = [NSURL fileURLWithPath:@"path to new pdf"];
CGContextRef destPDFContext = CGPDFContextCreateWithURL((__bridge CFURLRef)destURL, NULL, NULL);
CGContextSelectFont(destPDFContext, "CourierNewPS-BoldMT", 12.0, kCGEncodingFontSpecific);
// Copy the first page of the source pdf into the destination pdf
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(sourceDoc, 1);
CGRect pdfCropBoxRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
CGContextBeginPage (destPDFContext, &pdfCropBoxRect);
CGContextDrawPDFPage(destPDFContext, pdfPage);
// Close the source file
CGPDFDocumentRelease(sourceDoc);
// Draw the text
const char *text = "second line!";
CGContextShowTextAtPoint(destPDFContext, 10.0, 30.0, text, strlen(text));
// Close the destination file
CGContextRelease(destPDFContext);
}
This one is going to be really tricky.
Quartz doesn't really help you here.
You may wanna try libHaru, which has a pretty pessimistic license and is appstore compatible. It's not a free ride though - HARU can only generate pdfs.
http://libharu.org/