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