Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add annotation to pdf

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
like image 678
cancerian Avatar asked Aug 02 '11 15:08

cancerian


Video Answer


2 Answers

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);
}
like image 159
lnafziger Avatar answered Oct 10 '22 04:10

lnafziger


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/

like image 38
steipete Avatar answered Oct 10 '22 03:10

steipete