class WatermarkPage: PDFPage {
// 3. Override PDFPage custom draw
/// - Tag: OverrideDraw
override func draw(with box: PDFDisplayBox, to context: CGContext) {
// Holding the char positon
var rect = CGRect()
// Draw original content
super.draw(with: box, to: context)
// Draw rotated overlay string
UIGraphicsPushContext(context)
context.saveGState()
if let doc = document, let page = doc.page(at: 0){
rect = page.characterBounds(at: 1)
}
let pageBounds = self.bounds(for: box)
context.translateBy(x: 0.0, y: pageBounds.size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.rotate(by: CGFloat.pi / 4.0)
let string: NSString = "U s e r 3 1 4 1 5 9"
let attributes = [
NSAttributedStringKey.foregroundColor: UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.5),
NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 64)
]
string.draw(at: CGPoint(x:250, y:40), withAttributes: attributes)
context.fill(rect)
context.restoreGState()
UIGraphicsPopContext()
}
}
我不知道为什么在任何地方都要绘制可重定向的字符,但在文本位置的第一个字符中。这些是正中位置和尺寸(108.0,708.02490237,5.83959957000002,11.73046875)。
有人用过PDFKit吗?有没有人能找到出什么问题的线索?
发布于 2018-06-11 14:25:54
好的我找到了答案,坐标系统出了问题。字符位置是正确的,要用实际的坐标系绘制它,我必须从文档的高度和字符的高度减去它。
CGRect(x: char.x, y: page.height - char.y - char.height, width: char.width, height: char.height)
https://stackoverflow.com/questions/-100004832
复制相似问题