【问题标题】:programmatically create links in a PDF file以编程方式在 PDF 文件中创建链接
【发布时间】:2012-04-25 07:27:02
【问题描述】:

虽然我认为这是一个基本问题,但我还没有找到有效的答案。我正在通过抚摸 PDF 上下文的路径来创建 PDF 文件,并且我希望绘图上的不同区域成为外部内容的超链接 (http://bla.bla)。即使是不相交的矩形区域,我也会很高兴。有人知道怎么做吗?

【问题讨论】:

    标签: objective-c hyperlink pdf-generation


    【解决方案1】:

    检查这个问题的答案是否有效: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);
    }
    

    【讨论】:

    • 方法drawTextLink方法中如何传递frameRect?
    猜你喜欢
    • 2011-01-03
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2011-12-12
    相关资源
    最近更新 更多