【问题标题】:Embed hyperlink in PDF using Core Graphics on iOS在 iOS 上使用 Core Graphics 在 PDF 中嵌入超链接
【发布时间】:2013-01-22 18:44:08
【问题描述】:

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

我确信使用libharu 可以做到。我正在寻找的是使用 Core Graphics 做同样的事情,因为我的应用程序中已有的整个代码已经在使用这些方法。

== 编辑 ==

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

我正在使用类似的东西:

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

不过,矩形是不可点击的。

【问题讨论】:

    标签: ios pdf hyperlink core-graphics


    【解决方案1】:

    好的,我设法弄清楚它为什么不起作用。

    核心图形上下文是“反转”的,即原点位于页面的左下角,而 UIKit 的原点位于左上角。

    这是我想出的方法:

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

    这个方法的作用是:

    • 获取当前上下文并对提供的矩形应用转换,以便在 UIGraphicsSetPDFContextURLForRect 将其标记为可点击时获得一个可以在标记框时工作的矩形
    • 使用上述方法将新矩形 (xformRect) 标记为可点击
    • 保存当前上下文,以便以后所做的任何事情(颜色、大小、属性等)都不会在当前上下文中保持持久性
    • 在提供的矩形中绘制文本(现在使用 UIKit 坐标系)
    • 恢复上下文 GState

    【讨论】:

      【解决方案2】:

      这是 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()
      

      【讨论】:

        猜你喜欢
        • 2011-05-04
        • 1970-01-01
        • 1970-01-01
        • 2015-11-10
        • 1970-01-01
        • 1970-01-01
        • 2015-11-17
        • 1970-01-01
        • 2011-10-12
        相关资源
        最近更新 更多