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