user1258240
user1258240

Reputation: 855

programmatically create links in a PDF file

While I think it is a basic question, I didn't manage to find a response that works yet. I am creating a PDF file by stroking paths to a PDF context, and I want different areas on the drawing to be hyperlinks to outside contents (http://bla.bla). I'd be happy even with areas that are non-intersecting rectangles. Anyone knows how to do that?

Upvotes: 2

Views: 1769

Answers (1)

iosdev1111
iosdev1111

Reputation: 1112

check the answer to this question it works:Embed hyperlink in PDF using Core Graphics on iOS.

- (void) drawTextLink:(NSString *) text inFrame:(CGRect) frameRect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGAffineTransform ctm = CGContextGetCTM(context);

    // Translate the origin to the bottom left.
    // Notice that 842 is the size of the PDF page. 
    CGAffineTransformTranslate(ctm, 0.0, 842);

    // Flip the handedness of the coordinate system back to right handed.
    CGAffineTransformScale(ctm, 1.0, -1.0);

    // Convert the update rectangle to the new coordiante system.
    CGRect xformRect = CGRectApplyAffineTransform(frameRect, ctm);

    NSURL *url = [NSURL URLWithString:text];        
    UIGraphicsSetPDFContextURLForRect( url, xformRect );

    CGContextSaveGState(context);
    NSDictionary *attributesDict;
    NSMutableAttributedString *attString;

    NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
    attributesDict = @{NSUnderlineStyleAttributeName : underline, NSForegroundColorAttributeName : [UIColor blueColor]};
    attString = [[NSMutableAttributedString alloc] initWithString:url.absoluteString attributes:attributesDict];

    [attString drawInRect:frameRect];

    CGContextRestoreGState(context);
}

Upvotes: 4

Related Questions