【问题标题】:How to draw a signature or add an image signature to a PDF in Swift如何在 Swift 中为 PDF 绘制签名或添加图像签名
【发布时间】:2021-01-27 18:54:07
【问题描述】:

通过使用下面的代码,我可以加载 pdf。然后我在导航栏添加了一个添加签名按钮。单击添加签名按钮应该允许我浏览签名图像,然后单击添加按钮后它应该在 pdf 上添加签名并返回到第一个视图控制器。

或在第一个视图控制器中,当 pdf 显示如何绘制签名时

import UIKit 
import PDFKit

class ViewController: UIViewController { @IBOutlet weak var pdfContainerView: PDFView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "PDF Viewer"
    setupPdfView()
}

func setupPdfView() {
  
    if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") {
        if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
            pdfContainerView.displayMode = .singlePageContinuous
            pdfContainerView.autoScales = true
            pdfContainerView.displayDirection = .vertical
            pdfContainerView.document = pdfDocument
        }
    }
}
}

【问题讨论】:

  • This 可能会有所帮助

标签: ios swift pdf draw pdfkit


【解决方案1】:

您可以将 Apple 的 build in pdf 预览与 build it 注释一起使用,其中还包括用户的签名。 这篇文章解释了如何实现 QLPreviewViewControllerhttps://stackoverflow.com/a/68743098/12035498

【讨论】:

    【解决方案2】:

    添加文件ImageStampAnnotation.swift

    class ImageStampAnnotation: PDFAnnotation {
    var image: UIImage!
    
    init(with image: UIImage!, forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
        super.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp,  withProperties: properties)
        self.image = image
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func draw(with box: PDFDisplayBox, in context: CGContext)   {
        guard let cgImage = self.image.cgImage else { return }
        context.draw(cgImage, in: self.bounds)
    }
    }
    

    在pdf当前页设置注解

    func setupPdfView() {
        
        if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") {
            if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
                pdfContainerView.displayMode = .singlePageContinuous
                pdfContainerView.autoScales = true
                pdfContainerView.displayDirection = .vertical
                pdfContainerView.document = pdfDocument
                
                // position of sign image
                let rect = CGRect(x: 10, y: 10, width: 200, height: 35)
                let imageAnnotation = ImageStampAnnotation(with: UIImage(named: "signer"), forBounds: rect, withProperties: nil)
                pdfContainerView.currentPage?.addAnnotation(imageAnnotation)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-31
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多