8

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 PDFKit(which is only for desktop). I want the user to be able to add annotations and highlight text sections.

How do I go about with this? So far, I have parsed the pdf content (my previous post is pdf parsing problem). Is there any open source pdf editor which adds annotation to existing pdf???

Also, can this be done using Objective-C or is there any other language code (C or javascript) which does this?

NSURL *newUrl = [NSURL fileURLWithPath:"path to pdf"];
CGPDFDocumentRef templateDocument = CGPDFDocumentCreateWithURL(newUrl); 
CFRelease(url); 

CGContextRef pdfContext = UIGraphicsGetCurrentContext();
CGPDFPageRef page = CGPDFDocumentGetPage(templateDocument, 1);
CGContextDrawPDFPage(pdfContext, page);

const char *text = "second line!";
CGContextShowTextAtPoint (pdfContext, 10, 30, text, strlen(text));
CGContextEndPage (pdfContext);

CGContextRelease (pdfContext);

pdfcontext is the same object I created while creating a new pdf. My pdf has a line like "hello world". I'm trying to add one more line.

I am getting the following error:

GContextShowTextAtPoint: invalid context 0x0
Community
  • 1
  • 1
cancerian
  • 942
  • 1
  • 10
  • 18

2 Answers2

7

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);
}
lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • Great !! was really helpful. is there a tutorial or something for this. – Kunal Balani Mar 11 '14 at 15:15
  • @KunalBalani There may be, but none that I was involved with. If you have any questions that this doesn't address, feel free to ask your own question here! – lnafziger Mar 11 '14 at 15:19
  • @lnafziger your code will add read only text annotation to pdf. To make it editable on other viewer you need to make entry in /Annots dictionary of pdf. – Mayur Kothawade Dec 12 '14 at 08:03
  • @MayurKothawade, very good point. Please elaborate more on this. – Hemang May 10 '17 at 05:45
  • @Hemang You can refer http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf to study PDF format, You can follow incremental update format to add editable annotations – Mayur Kothawade May 11 '17 at 11:26
3

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/

steipete
  • 7,581
  • 5
  • 47
  • 81
  • 1
    hi thanks for reply but i need to edit the pdf thats my requirement. but is there any way to rewrite the whole contents of the pdf instead of just editing it. this is my code(edited my question). i tried but it shows error saying invalid context – cancerian Aug 03 '11 at 11:15