13

I have managed to implement a very basic PDF viewer within my application, but was wondering if it was possible to add annotations to the PDF. I have looked through the SDK docs, but not found anything. I have 2 questions really:

  1. Is it possible to do this?
  2. What is the best approach to take?
  3. Is there a framework or library that I can include to assist with this?

Thanks.

Jack
  • 3,878
  • 15
  • 42
  • 72

3 Answers3

19

You can do annotation by reading in a PDF page, drawing it onto a new PDF graphics context, then drawing extra content onto that graphic context. Here is some code that adds the words 'Example annotation' at position (100.0,100.0) to an existing PDF. The method getPDFFileName returns the path of the original PD. getTempPDFFileName returns the path of the new PDF, the one that is the original plus the annotation.

To vary the annotations, just add in more drawing code in place of the drawInRect:withFont: method. See the Drawing and Printing Guide for iOS for more on how to do that.

- (void) exampleAnnotation;
{
    NSURL* url = [NSURL fileURLWithPath:[self getPDFFileName]];

    CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);// 2
    size_t count = CGPDFDocumentGetNumberOfPages (document);// 3

    if (count == 0)
    {
        NSLog(@"PDF needs at least one page");
        return;
    }

    CGRect paperSize = CGRectMake(0.0,0.0,595.28,841.89);

    UIGraphicsBeginPDFContextToFile([self getTempPDFFileName], paperSize, nil);

    UIGraphicsBeginPDFPageWithInfo(paperSize, nil);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    // flip context so page is right way up
    CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
    CGContextScaleCTM(currentContext, 1.0, -1.0); 

    CGPDFPageRef page = CGPDFDocumentGetPage (document, 1); // grab page 1 of the PDF 

    CGContextDrawPDFPage (currentContext, page); // draw page 1 into graphics context

     // flip context so annotations are right way up
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextTranslateCTM(currentContext, 0, -paperSize.size.height);

    [@"Example annotation" drawInRect:CGRectMake(100.0, 100.0, 200.0, 40.0) withFont:[UIFont systemFontOfSize:18.0]];

    UIGraphicsEndPDFContext();

    CGPDFDocumentRelease (document);
}
Obliquely
  • 7,002
  • 2
  • 32
  • 51
  • 2
    This has been a massive help. Once I've got a deeper understanding of this, I'll create a github project to share with the community. – Jack Jun 17 '11 at 20:46
  • Did you make a GitHub project @Jack? – Adam Waite Feb 20 '12 at 17:20
  • @Metthew I tried your example but I cant open the document. I get a 'Permission denied' error. – Justin Jun 18 '12 at 11:36
  • @Juzzz: it may be because of the pdf you try to modify is password protected – Lithu T.V Sep 06 '12 at 12:32
  • @Obliquely your code will add text annotation in pdf page, but will it be modifiable by other applications like adobe or MAC's preview? as you are just drawing annotation on pdf page instead of making any entry for that annotation in /Annots dictionary of pdf. – Mayur Kothawade Dec 12 '14 at 06:15
  • @Obliquely This draws to the PDF not adding an annotation like what Mayur Kothawade say. I think that we have to parse the pdf ourselves, then add the annotation using the PDF sepcs – Khaled Annajar Jun 03 '15 at 09:08
3

I am working in the stuff and created a GitHub project. Please check it out here.

lazyprogram
  • 139
  • 1
  • 4
  • Your project however does not save the annotations to the pdf . I checked the source code , it is archiving all the annotations info independent of the actual pdf files . So If I pass on the pdf file to another machine then the pdf is transferred without any changes . – Bharat Jagtap Feb 27 '15 at 07:25
  • It is under development. Please check out later :) – lazyprogram Mar 02 '15 at 05:55
  • @lazyprogram, kindly, update its library as I am getting dylib error and the app is crashing on the device when running it from xcode 7. – Md Rais Dec 16 '15 at 13:34
  • Hi, I used your project and it is working fine. But now I need to move the textviews or drawn images with my hand like a skitch app. How to implement that ? Can you provide any start ? – Sharad Chauhan Jun 23 '16 at 13:17
1

I don't think PDFAnnotation or PDFKit were ported to iPhone from the desktop.... probably a great excuse to file a radar. However, Haru may get you by in the meantime.

slf
  • 22,595
  • 11
  • 77
  • 101
  • Currently, Haru is available to run on the following platforms only: 1. Cygwin + GCC (Microsoft Windows) 2. Cygwin + MinGW (Microsoft Windows) 3. MSYS + MinGW (Microsoft Windows) 3. Microsoft VC++ (Microsoft Windows) 4. Borland C++ (Microsoft Windows) 5. GCC (Linux, FreeBSD, NetBSD, Solaris...) – Md Rais Dec 08 '15 at 07:25