swiftpdfpdf-annotationsios-pdfkit

How do I disable to edit PDFAnnotations?


I'm builting iOS app in Swift 4.2 that can make signature to PDF file. But in case Embeding the signature as PDFAnnotation, it can editable in apple's Preview app or other app which can edit pdf. How can I disable to edit PDFAnnoations ??

"Signature" is creates by UIImage which converted handwriting 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)
}

Solution

  • I finally find a solution!

    The idea here is not using PDFAnnotation!.

    the Signature is an Image so when user save the PDF

    you will use the signature Image to create new PDF and save it to same PDF file

    to put your signature where you want it you need the modify X and Y image.draw(in: CGRect(x: 100, y: 100, width: 100, height: 100))

    the code below will get the Path of the PDF usually, it saved in Document Folder you just need to send the Path of the file and also the signature image and the function will add the image above the PDF and save it

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