Skip to content

Instantly share code, notes, and snippets.

@jkmathew
Created February 14, 2017 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkmathew/33f7dd40a1d3adc5a4ba5fbcad14d1b3 to your computer and use it in GitHub Desktop.
Save jkmathew/33f7dd40a1d3adc5a4ba5fbcad14d1b3 to your computer and use it in GitHub Desktop.
//reference https://gist.github.com/tempire/0bd8cb78290aa9bc6adf
func addSignature(_ signature:UIImage, onPDFAt path: String) {
let pdf = CGPDFDocument(NSURL(fileURLWithPath: path))!
let pageCount = pdf.numberOfPages;
UIGraphicsBeginPDFContextToFile(path, CGRect.zero, nil)
for index in 1...pageCount {
let page = pdf.page(at: index)!
let pageFrame = page.getBoxRect(CGPDFBox.mediaBox)
UIGraphicsBeginPDFPageWithInfo(pageFrame, nil)
let ctx = UIGraphicsGetCurrentContext()!
ctx.saveGState();
ctx.scaleBy(x: 1, y: -1);
ctx.translateBy(x: 0, y: -pageFrame.size.height);
ctx.drawPDFPage(page);
ctx.restoreGState();
signature.draw(in: CGRect(x: 492, y: 652, width:100 , height: 100))//change accordingly
}
UIGraphicsEndPDFContext()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment