Swift PDFkit将图像放入图像字段



我有一个带有Image Field的pdf,在swift中我有一份UIImage,如何在Image Field注释所在的pdf上绘制UIImage

尝试1

我尝试将图像的注释上的一些字段转换为base64字符串,但没有成功。

let image: UIImage = UIImage.shapeImageWithBezierPath(bezierPath: path, fillColor: .white, strokeColor: .black)
let data = image.jpegData(compressionQuality: 0.5)
annotation.widgetStringValue = String(data?.base64EncodedString() ?? "")
annotation.contents = String(data?.base64EncodedString() ?? "")

尝试2

我也发现了这个类似的问题。无法使用PDFKit添加图像并保存到pdf文件,但它会创建一个新的注释并将其放置在页面顶部,而我需要将图像放置在pdf上的注释中或顶部。

使用上面的问题,我尝试了下面的问题,但没有任何运气。

let image: UIImage = UIImage.shapeImageWithBezierPath(bezierPath: path, fillColor: .white, strokeColor: .black)
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
annotation.page?.addAnnotation(imageStamp)

尝试3

我也试过像下面这样在注释上绘制图像,但它没有出现在pdf上。

let image: UIImage = UIImage.shapeImageWithBezierPath(bezierPath: path, fillColor: .white, strokeColor: .black)
image.draw(in: annotation.accessibilityFrame)

尝试4

我发现我可以用下面的代码在注释上绘制UIBezierPath,但我的图像必须保存为数据库中的base64,无法转换回UIBezierPath,所以我仍然需要确定如何在注释上画图像。

annotation.type = "Ink"
annotation.color = UIColor(.black)
annotation.add(path) //path being my `UIBezierPath`

谢谢你的帮助!!!!

我能够在注释图像边界内绘制图像,然后通过编写此类来替换注释

final private class PDFImageAnnotation: PDFAnnotation {
var image: UIImage?
convenience init(image: UIImage?, properties: [AnyHashable: Any]?, rect: CGRect) {
//set bounds & image
self.init(bounds: rect, forType: .stamp, withProperties: properties)
self.image = image
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
//flip image context if upside down
//context.translateBy(x: 0.0, y: 235) //y depends on image height, image will disappear (out of bounds) if not set correctly
//context.scaleBy(x: 1.0, y: -1.0)
//draw image
guard let cgImage = image?.cgImage else { return }
context.draw(cgImage, in: self.bounds)
}
}

在我的pdf生成过程中调用这个

//make UIImage from base64
let uiImage = UIImage(data: Data(base64Encoded: String(data?.base64EncodedString() ?? ""))!)
//get image bounds from current pdf annotation
let imageBounds = CGRect(x: (annotation.startPoint.x * -1), y: (annotation.startPoint.y * -1), width: 100, height: 20)
//create image annotation with class
let imageStamp = PDFImageAnnotation(image: uiImage, properties: nil, rect: imageBounds)
//remove old annotation
page.removeAnnotation(annotation)
//add new annotation
imageStamp.fieldName = "CustomerSignature"
page.addAnnotation(imageStamp)

一个更好的解决方案可能是为PDF注释编写一个扩展类

extension PDFAnnotation {
func draw(with box: PDFDisplayBox, in context: CGContext, cgImage: CGImage) {
//flip image context if upside down
//context.translateBy(x: 0.0, y: 235) //y depends on image height, image will disappear (out of bounds) if not set correctly
//context.scaleBy(x: 1.0, y: -1.0)
context.scaleBy(x: 1.0, y: -1.0)
context.draw(cgImage, in: self.bounds)
}
}

但我不确定如何通过CGContext

annotation.draw(with: pdfView.displayBox, in ????, cgImage: cgImage)

最新更新