iOS在创建PDF时添加链接 [英] iOS Add links while creating PDF

查看:139
本文介绍了iOS在创建PDF时添加链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iOS中使用UIGraphicsBeginPDFContextToFile创建PDF,我想在其他页面以及其他网站上添加URL链接。下面是代码:这里pdfObj是一个自定义类,它将文本绘制为PDF。在成功创建PDF后,如果单击转到第1章和转到URL区域没有任何反应

I am creating a PDF using UIGraphicsBeginPDFContextToFile in iOS, where I want to add URL links to other pages as well as other websites. Below is the code : here pdfObj is a custom class which draws text to PDF. After Succesfull creation of PDF nothing happens if I clicks the area for "GO to Chapter1" and "GO to URL"

UIGraphicsBeginPDFContextToFile(documentDirectoryFilename, CGRectZero, nil);
UIGraphicsBeginPDFPage();

//Page 1
UIGraphicsAddPDFContextDestinationAtPoint(@"Chapter1", CGPointMake(0, 0));
//other draw pdf data....

//Page 2
UIGraphicsBeginPDFPage();
//other draw pdf data....
[pdfObj addHeaderText:@"Go to Chapter 1" atFrame:CGRectMake(50, 50, 200, 44)];
UIGraphicsSetPDFContextDestinationForRect(@"Chapter1", CGRectMake(50, 50, 200, 44));

[pdfObj addHeaderText:@"Go to URL" atFrame:CGRectMake(550, 550, 200, 44)];
UIGraphicsSetPDFContextURLForRect([NSURL urlWithString:@"http://www.myURL.com"], CGRectMake(550, 550, 200, 44));

UIGraphicsEndPDFContext();

请告诉我这里的错误

推荐答案

检查这个问题的答案是否有效:使用iOS上的Core Graphics在PDF中嵌入超链接

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

这篇关于iOS在创建PDF时添加链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆