I have a class to edit pdf by inserting an image into the pdf and save a new pdf with the inserted image.
Below code is the way I use to achieve the scenario, by creating a custom PDFAnnotation
. I specify type as .widget
instead of .stamp
to avoid having black line drawing across diagonal.
final private class PDFImageAnnotation: PDFAnnotation {
var image: UIImage?
convenience init(_ image: UIImage?, bounds: CGRect, properties: [AnyHashable: Any]?) {
self.init(bounds: bounds, forType: PDFAnnotationSubtype.widget, withProperties: properties)
self.image = image
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
super.draw(with: box, in: context)
// Drawing the image within the annotation's bounds.
guard let cgImage = image?.cgImage else { return }
context.draw(cgImage, in: bounds)
}
}
Below is how I display pdf and select image to save in pdf
private func setupPDF() {
guard let url = url else { return }
pdfView.document = PDFDocument(url: url)
pdfView.autoScales = true
}
private func addImageAndSave(_ image: UIImage) {
guard let page = pdfView.currentPage else { return }
let pageBounds = page.bounds(for: .cropBox)
let imageBounds = CGRect(x: pageBounds.midX, y: pageBounds.midY, width: image.size.width, height: image.size.height)
let imageStamp = PDFImageAnnotation(image, bounds: imageBounds, properties: nil)
imageStamp.shouldDisplay = true
imageStamp.shouldPrint = true
page.addAnnotation(imageStamp)
// Save PDF with image
let fileName = url.lastPathComponent
let saveUrl = FileManager.default.temporaryDirectory.appendingPathComponent(fileName, isDirectory: false)
pdfView.document?.write(to: saveUrl)
}
However the result pdf file is below
The preview on right column does display the picture but when open the pdf in Preview
app or any browser the picture is not there.
How can I make the picture appear in the final pdf file?
Thanks.