首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用PDFKit在位置中添加字符的边框?

如何使用PDFKit在位置中添加字符的边框?
EN

Stack Overflow用户
提问于 2018-06-11 05:05:10
回答 1查看 0关注 0票数 0
代码语言:txt
复制
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吗?有没有人能找到出什么问题的线索?

EN

回答 1

Stack Overflow用户

发布于 2018-06-11 14:25:54

好的我找到了答案,坐标系统出了问题。字符位置是正确的,要用实际的坐标系绘制它,我必须从文档的高度和字符的高度减去它。

代码语言:txt
复制
CGRect(x: char.x, y: page.height - char.y - char.height, width: char.width, height: char.height)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100004832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档