使用iOS上的Core Graphics在PDF中嵌入超链接 [英] Embed hyperlink in PDF using Core Graphics on iOS

查看:100
本文介绍了使用iOS上的Core Graphics在PDF中嵌入超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一件非常简单的事情:在用户实际点击的PDF文件中写一个URL。

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

我肯定知道使用 libharu 即可完成。我正在寻找的是使用Core Graphics做同样的事情,因为我在我的应用程序中已有的整个代码已经在使用这些方法。

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 ==

== edit ==

我想我找到了一些东西: UIGraphicsSetPDFContextURLForRect 但我无法让它起作用。

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

我使用的是:

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

但是,矩形不可点击。

推荐答案

好的,我设法找出它无法正常工作的原因。

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

核心图形上下文在某种意义上是逆转的当UIKit的原点位于左上角时,原点位于页面的左下角。

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.

这是我提出的方法:

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

这种方法的作用是:


  • 获取当前上下文并对提供的rect应用转换,以获得在 UIGraphicsSetPDFContextURLForRect 将其标记为可点击

  • 以使用上述方法将新矩形( xformRect )标记为可点击

  • 保存当前上下文,以便以后执行的任何操作(颜色,大小,属性等)在当前上下文中不会保持持久性

  • 以绘制文本提供的rect(现在使用UIKit坐标系)

  • 恢复上下文GState

  • 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

这篇关于使用iOS上的Core Graphics在PDF中嵌入超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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