Skip to content

Instantly share code, notes, and snippets.

@kmithi1-apps
Created December 6, 2024 17:21
Show Gist options
  • Save kmithi1-apps/02eb54ebf1dfaa1cced35f18ed246fdb to your computer and use it in GitHub Desktop.
Save kmithi1-apps/02eb54ebf1dfaa1cced35f18ed246fdb to your computer and use it in GitHub Desktop.
Add stamp annotation to PDF page.
class PDFImageAnnotation: PDFAnnotation {
var image: UIImage?
convenience init(_ image: UIImage?, bounds: CGRect, properties: [AnyHashable : Any]?) {
self.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp, withProperties: properties)
self.image = image
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
super.draw(with: box, in: context)
guard let cgImage = image?.cgImage else { return }
context.draw(cgImage, in: bounds)
}
}
// add annotation
let image = UIImage(named: "myLogo")
let imageAnnotation = PDFImageAnnotation(image, bounds: CGRect(x: 200, y: 200, width: 200, height: 200), properties: nil)
page.addAnnotation(imageAnnotation) // Note make sure this in memory page object is not shared among multiple document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment