Search code examples
iosparsingpdfannotationsquartz-2d

Can I programmatically add link annotation to PDFs?


I am making an iPad app that uses UIWebView to display PDFs. I have a PDF that I would like to programmatically add links to. For simplicity, lets say there are 10 paragraphs. They are all numbered and have a few lines of text in them. I want to be able to somehow add a basic link to the PDF so that if paragraph 2 is touched, then my UIWebView can process the request that is associated with paragraph 2.

I have no idea what the structure of the PDF is like on the inside. I have no clue how to scale this to each paragraph of several hundred pages. But I am wondering if I can somehow add a link or HTML to the PDF so that I can manipulate it with my app.

Thanks!

To be clear, I am viewing this PDF on an iOS device but I recognize that the solution to my question might not have anything to do with Cocoa-touch frameworks. I am looking for any sort of solution that will allow me to add invisible links to certain areas of my PDF.


Solution

  • If you want an iPad app to recognize text fields, buttons, links from a pdf. You can edit the actual pdf (you'll need a version of Adobe Acrobat) and add those fields to the pdf. In your ios code parse the pdf fields using something like:

    in a parse method:

        -(void)parse:(CGPDFPageRef)page
    {
        for (int i = 0; i < CGPDFArrayGetCount (annotations); i++)
    

    CGPDFArrayGetCount returns the number of items in a PDF array.

    in the loop grab the field name:

        if (CGPDFDictionaryGetString(dict, "T", &stringRef))
        {
            char *s = (char *) CGPDFStringGetBytePtr(stringRef);
            fieldName = [NSString stringWithCString:s encoding:NSUTF8StringEncoding];
        }
    

    check to see what you want to do if a field name matches, say "button_1" or "hlink_3":

        if ([fieldName isEqualToString:@"hlink_3"])
    {
            // do whatever, example add a button where the field was
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = rect;
            [self addSubview:button];
    }
    

    There's a lot more to it, but this is the general idea.