0

I followed this article

to signature the PDF using PDFAnnotation

you can download the project using in the article here

my problem is because it's PDFAnnotation if I download the pdf on my computer and then open it with Preview or any PDFviewer app I can move the PDFAnnotation around the page!

and because my app is Client to Client

so Client 1 signature the PDF and then send it to Client 2 and also Client 2 signature the PDF

that why I need to render new pdf, that means the PDFAnnotation became within the PDF, not as PDFAnnotation

Also, you can download this PDF

you will notice my problem and how the two PDFAnnotations can be moved around

Basel
  • 550
  • 8
  • 21
  • I would suggest to use the isReadOnly property for the PDFAnnototation. Maybe this helps https://stackoverflow.com/questions/49941644/ios-pdfkit-make-text-widget-pdfannotation-readonly – Klinki Feb 04 '19 at 12:22
  • @Klinki I did try it , it kinda work inside the app , when I open the old PDF I can't move the PDFAnnotations but if I add another PDFAnnotations it also won't move after added it ! Also if I download the PDF it will still can move the PDFAnnotations The Example PDF above is using same code on your link – Basel Feb 04 '19 at 12:58
  • HI, can you please share the latest and working code sample. I want to implement the same. If possible can share GitHub link with new/updated working code sample – Nikita Patil Feb 03 '22 at 15:34

1 Answers1

0

I finally find a solution!

Thanks to tempire and his code

I did convert it to Swift 4.2

The idea here is not using PDFAnnotation!.

the Signature is an Image so when user save the PDF

I 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))

   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()
    }
Basel
  • 550
  • 8
  • 21
  • HI, can you please share the latest and working code sample. I want to implement the same. If possible can share GitHub link with new/updated working code sample – Nikita Patil Feb 03 '22 at 15:34
  • @NikitaPatil I had Demo project on github : https://github.com/X901/BurnImageToPDF – Basel Feb 04 '22 at 20:43