Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed hyperlink in PDF using Core Graphics on iOS

I'm trying to do a quite simple thing: write an URL inside a PDF file that can be actually clicked by the user.

I know for sure that using libharu it can be done. What I'm looking for is to do the same using Core Graphics since the whole code I already have in my app is already using those methods.

== edit ==

I think I found something: UIGraphicsSetPDFContextURLForRect but I can't make it to work.

I'm using something like:

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
UIGraphicsSetPDFContextURLForRect( url, CGRectMake(0, 0, 100, 100));

The rect is not clickable, though.

like image 487
Fabiano Francesconi Avatar asked Feb 07 '13 09:02

Fabiano Francesconi


2 Answers

Ok I managed to figure out why it wasn't working.

Core Graphics context are "reversed" in the sense of having the origin at the bottom left of the page while UIKit has the origin in the top-left corner.

This is the method I came up with:

- (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);
}

What this method does is:

  • to get the current context and apply a transformation to the provided rect so to obtain a rect that would work when marking the box when the UIGraphicsSetPDFContextURLForRect will mark it as clickable
  • to mark the new rect (xformRect) as clickable using the aforementioned method
  • to save the current context so whatever is done later (colour, size, attributes, whatever) do not remain persistent in the current context
  • to draw the text in the provided rect (now using the UIKit coordinate system)
  • to restore the context GState
like image 90
Fabiano Francesconi Avatar answered Sep 21 '22 23:09

Fabiano Francesconi


Here is converted code for Swift 5

let context = UIGraphicsGetCurrentContext()
let ctm = context?.ctm

// Translate the origin to the bottom left.
// Notice that 842 is the size of the PDF page. 
ctm?.translatedBy(x: 0.0, y: 842)

// Flip the handedness of the coordinate system back to right handed.
ctm?.scaledBy(x: 1.0, y: -1.0)

var xformRect: CGRect? = nil
if let ctm = ctm {
    xformRect = frameRect.applying(ctm)
}

let url = URL(string: text)
if let url = url {
    UIGraphicsSetPDFContextURLForRect(url, xformRect ?? CGRect.zero)
}

context?.saveGState()

let attributesDict =[
        .foregroundColor: UIColor.blue,
        .underlineStyle: NSUnderlineStyle.single.rawValue
    ]
let attString = NSMutableAttributedString(string: url?.absoluteString ?? "", attributes: attributesDict as? [NSAttributedString.Key : Any])

attString?.draw(in: frameRect)

context?.restoreGState()
like image 25
Mitesh Avatar answered Sep 22 '22 23:09

Mitesh