Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for how-to use UIGraphicsSetPDFContextURLForRect on iOS or Cocoa

I'm playing to manipulate PDF on iOS (display but also and mostly generation).

I wish to embed rectangular areas which act as "external" links (URLs like http://host.tld/path/file).

Do you know where I can find an example(s) of how to use the UIGraphicsSetPDFContextURLForRect function? I find absolutly nothing on Internet.

If I understand well, the only requirement is that the current graphic context should be PDF "type" and I think I respect this since, upstream, I call UIGraphicsPushContext why my PDF context as parameter (this was anyway required for methods like drawAtPoint: that I use also with success).

I do not think it is important but just in case I specify that this DOES NOT take place in the drawRect: of a View subclass.

You're quite right to say me unequivocally that I have "all wrong". The graphical environment is so sophisticated and rich in iOS that I assume to have not assimilated more than 1 or 2% for now.

Thank you in advance.

like image 703
Diablotin Avatar asked Sep 16 '12 22:09

Diablotin


2 Answers

You are right, the requirement is that current graphic context is a PDF context. You can either push it (UIGraphicsPushContext) to make it active or you create the context like this:

UIGraphicsBeginPDFContextToFile(path, CGRectMake(0, 0, 612, 792), nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
// ...
UIGraphicsSetPDFContextURLForRect(url, rect);
// ... 
UIGraphicsEndPDFContext(); 

The UIGraphicsSetPDFContextURLForRect has 2 parameters: first is the url where you want the link to go, the 2nd is the rectangle on the current page that represents the link area. For example this line will create link to a url in the bottom left corner of the page, the size of the link is 72*72 points:

UIGraphicsSetPDFContextURLForRect(url, CGRectMake(0, 0, 72, 72));

What this method actually does is to create a link annotation in the PDF file. The link itself will have no visual appearance, you have to know it is there or you might discover it by chance if you move the mouse over the PDF page in the viewer. Because of this you usually draw/write something on the page (e.g. Click Here) and you set the link area above the text.

Update - working code fragment:

- (void) makePdf {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFile = [documentsDirectory stringByAppendingPathComponent:@"uigraphics.pdf"];

    UIGraphicsBeginPDFContextToFile(pdfFile, CGRectMake(0, 0, 612, 792), nil);
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

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

    UIGraphicsEndPDFContext();
}
like image 153
iPDFdev Avatar answered Oct 14 '22 16:10

iPDFdev


I hope this will help You.

UIGraphicsBeginPDFContextToFile( pdfFilePath, CGRectZero, nil );

CGRect windowFrame = CGRectMake( 0, 0, 596, 842 );
UIGraphicsBeginPDFPageWithInfo( windowFrame, nil );

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];

CGRect textFrame = CGRectMake( 10, 10, 200, 20 );
CGRect linkFrame = textFrame;
linkFrame.origin.y = windowFrame.size.height - linkFrame.origin.y - linkFrame.size.height;
UIGraphicsSetPDFContextURLForRect( url, linkFrame );

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:textFrame];

UIGraphicsEndPDFContext();
like image 37
Ned Avatar answered Oct 14 '22 18:10

Ned