【问题标题】:How do I disable to edit PDFAnnotations?如何禁用编辑 PDFAnnotations?
【发布时间】:2019-06-14 10:02:56
【问题描述】:

我正在使用 Swift 4.2 构建 iOS 应用程序,可以对 PDF 文件进行签名。 但是如果将签名嵌入为 PDFAnnotation,它可以在苹果的 Preview 应用程序或其他可以编辑 pdf 的应用程序中进行编辑。 如何禁用编辑 PDFAnnoations ??

“签名”是由 UIImage 创建的,它转换了手写 beizerpaths。

class SignatureAnnotation: PDFAnnotation {

    var image: UIImage!

    init(with image: UIImage!, forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
        super.init(bounds: bounds,
                   forType: PDFAnnotationSubtype.freeText,
                   withProperties: properties) 
        self.image = image
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }

    override func draw(with box: PDFDisplayBox, in context: CGContext) {
        guard let cgImage = self.image.cgImage else { return }
        context.draw(cgImage, in: self.bounds)
    }

}

func embedSignatureToPDF() {
    let page = pdfView.currentPage
    let signatureAnnotation = SignatureAnnotation(with: signature, forBounds: signatureRect, withProperties: nil)
        page.addAnnotation(signatureAnnotation)
}

【问题讨论】:

  • 你找到答案了吗? ,我在我的应用中注意到了这种行为,但我没有找到任何解决方案来解决它
  • @Basil 你好,Basil。不,还没有解决...

标签: swift pdf pdf-annotations ios-pdfkit


【解决方案1】:

我终于找到了解决办法!

这里的想法不是使用PDFAnnotation!。

签名是图像 所以当用户保存 PDF 时

您将使用签名图像 创建新的 PDF 并将其保存到同一个 PDF 文件中

把你的签名放在你想要的地方 你需要修改 XY image.draw(in: CGRect(x: 100, y: 100, width: 100, height: 100))

下面的代码将获取 PDF 的路径 通常,它保存在文档文件夹中 你只需要发送文件的路径 还有签名图片 该功能将在PDF上方添加图像并保存

   func drawOnPDF(path: String , signatureImage:UIImage) {

        // Get existing Pdf reference
    let pdf = CGPDFDocument(NSURL(fileURLWithPath: path))

        // Get page count of pdf, so we can loop through pages and draw them accordingly
    let pageCount = pdf?.numberOfPages


        // Write to file
        UIGraphicsBeginPDFContextToFile(path, CGRect.zero, nil)

        // Write to data
        //var data = NSMutableData()
        //UIGraphicsBeginPDFContextToData(data, CGRectZero, nil)

        for index in 1...pageCount! {

            let page =  pdf?.page(at: index)

            let pageFrame = page?.getBoxRect(.mediaBox)


            UIGraphicsBeginPDFPageWithInfo(pageFrame!, nil)

            let ctx = UIGraphicsGetCurrentContext()

            // Draw existing page
            ctx!.saveGState()

            ctx!.scaleBy(x: 1, y: -1)

            ctx!.translateBy(x: 0, y: -pageFrame!.size.height)
            //CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
            ctx!.drawPDFPage(page!)
            ctx!.restoreGState()

            // Draw image on top of page
            let image = signatureImage
            image.draw(in: CGRect(x: 100, y: 100, width: 100, height: 100))
            // Draw red box on top of page
            //UIColor.redColor().set()
            //UIRectFill(CGRectMake(20, 20, 100, 100));
        }


        UIGraphicsEndPDFContext()
    }

【讨论】:

  • 谢谢@Basil!我认为只有一种方法可以不使用 PDF Annotation。可以在哪个PDF页面上添加签名。这是一个非常大的帮助
  • 很高兴为您提供帮助,您能接受答案吗?
【解决方案2】:

将注解类型改为按钮怎么样?,每次触摸签名都会有一个覆盖但无法编辑

init(with image: UIImage!, forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
    super.init(bounds: bounds, forType: PDFAnnotationSubtype.widget, withProperties: properties)
    self.image = image
    self.isReadOnly = true
    self.widgetFieldType = .button
}

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 2012-01-09
    • 2020-10-26
    • 1970-01-01
    • 2020-02-06
    • 2011-08-10
    相关资源
    最近更新 更多