新PDF中的Rander PDFAnnotation

问题描述 投票:0回答:1

我跟着这个article

使用PDFAnnotation签名PDF

您可以在文章here中下载该项目

我的问题是因为它是PDFAnnotation如果我在我的计算机上下载pdf,然后使用Preview或任何PDFviewer应用程序打开它我可以在页面周围移动PDFAnnotation!

因为我的应用程序是Client to Client

因此,客户1签署PDF,然后将其发送给客户端2,并将客户端2签名为PDF

这就是为什么我需要渲染新的pdf,这意味着PDFAnnotation在PDF中,而不是PDFAnnotation

另外,你可以下载这个PDF

您会注意到我的问题以及如何移动两个PDFAnnotations

ios swift pdf pdfkit pdf-annotations
1个回答
0
投票

我终于找到了解决方案!

感谢tempire和他的code

我确实将它转换为Swift 4.2

这里的想法是不使用PDFAnnotation!。

签名是一个图像,因此当用户保存PDF时

我将使用签名图像创建新PDF并将其保存到相同的PDF文件

把你的签名放在你想要的地方你需要修改X和Y image.draw(in: CGRect(x: 100, y: 100, width: 100, height: 100))

   func drawOnPDF(path: String , signatureImage:UIImage) {

        // Get existing Pdf reference
    let pdf = CGPDFDocument(NSURL(fileURLWithPath: path))

        // Get page count of pdf, so we can loop through pages and draw them accordingly
    let pageCount = pdf?.numberOfPages


        // Write to file
        UIGraphicsBeginPDFContextToFile(path, CGRect.zero, nil)

        // Write to data
        //var data = NSMutableData()
        //UIGraphicsBeginPDFContextToData(data, CGRectZero, nil)

        for index in 1...pageCount! {

            let page =  pdf?.page(at: index)

            let pageFrame = page?.getBoxRect(.mediaBox)


            UIGraphicsBeginPDFPageWithInfo(pageFrame!, nil)

            let ctx = UIGraphicsGetCurrentContext()

            // Draw existing page
            ctx!.saveGState()

            ctx!.scaleBy(x: 1, y: -1)

            ctx!.translateBy(x: 0, y: -pageFrame!.size.height)
            //CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
            ctx!.drawPDFPage(page!)
            ctx!.restoreGState()

            // Draw image on top of page
            let image = signatureImage
            image.draw(in: CGRect(x: 100, y: 100, width: 100, height: 100))
            // Draw red box on top of page
            //UIColor.redColor().set()
            //UIRectFill(CGRectMake(20, 20, 100, 100));
        }


        UIGraphicsEndPDFContext()
    }
© www.soinside.com 2019 - 2024. All rights reserved.