iPhone, CGPDFDocument - PDF links
Asked Answered
L

1

24

I'm trying to write a simple PDF viewer using CGPDFDocument, based on QuartzDemo.
There is common rendering:

-(void)drawInContext:(CGContextRef)context
{
    // PDF page drawing expects a Lower-Left coordinate system, 
    // so we flip the coordinate system before we start drawing.
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    // Grab the first PDF page
    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, pageNumber);
    // We're about to modify the context CTM to draw the PDF page
    //  where we want it, so save the graphics state 
    // in case we want to do more drawing
    CGContextSaveGState(context);
    // CGPDFPageGetDrawingTransform provides an easy way 
    // to get the transform for a PDF page. It will scale down to fit, 
    // including any base rotations necessary to display the PDF page correctly. 
    CGAffineTransform pdfTransform = 
            CGPDFPageGetDrawingTransform(page, 
                    kCGPDFCropBox, self.bounds, 0, true);
    // And apply the transform.
    CGContextConcatCTM(context, pdfTransform);
    // Finally, we draw the page 
    // and restore the graphics state for further manipulations!
    CGContextDrawPDFPage(context, page);
    CGContextRestoreGState(context);
}

As I understand, its only drawing, so all structure navigation or outgoing links should be handled manually (ex. in touch event).

There are functions which will set URL or create element with URL.

Question is: how to get outgoing link URL from certain PDF block?

Thank you!

Similar questions:
PDF hyperlinks on iPhone/iPad
How to access hyperlinks in PDF documents (iPhone)?

Same on
iPhone SDK Dev GGroup
macRumors Forum
iPhone Dev SDK Forum
Dev Shed Forum
iphonedevbook.com

Leoleod answered 15/7, 2010 at 15:23 Comment(3)
+1 for a question with no answer anywhere. rubs head and keeps diggingEsperanzaespial
+1, 50 point bounty - I would love to know the answer too we have been searching for a few weeks now and no matter what we've tried no resultClandestine
+100 points for working sample!Leoleod
C
18

This is a non-trivial question, which is why you've found no easy answer. The solution is: you have to parse the PDF document. The good news is, there are functions provided by Apple to allow you do to this. The bad news is, they're procedural, and man, is it a convoluted mess.

In very rough pseudo code:

  • use CGPDFDocumentGetCatalog to get the catalog for the PDF
  • look in the "pages" element of the catalog for the page you're looking for (yes, it's a dictionary, not an array, and a nested one, so this simple operation is not-so-simple).
  • now use CGPDFDictionaryGetArray to get the "Annots" object
  • next, you need to iterate through the annotations and look for "Link" objects.

Adobe has published a document that lays out the PDF specification. Good luck!

Carnauba answered 28/9, 2010 at 0:27 Comment(1)
Thanks I realise this isnt a trivial question, hence the bounty, but it is very important and Apples documentation is very poor regarding Quartz PDF on the iPhone, and there are no other blogs or tutorials covering itClandestine

© 2022 - 2024 — McMap. All rights reserved.