繁体   English   中英

iOS PDFKit:使文本小部件 PDFAnnotation 只读

[英]iOS PDFKit: make Text Widget PDFAnnotation readonly

我想让 Text Widget PDFAnnotation 只读。 我试图将isReadOnly标志设置为true ,但似乎没有任何区别。 用户在点击注释后仍然可以对其进行编辑。

PDFKit 不支持注释上的 isReadOnly 属性似乎是一个错误/疏忽。 但是,我能够通过在文档中的其他注释上添加一个空白注释来解决这个问题。 我向 PDF 文档添加了一个makeReadOnly()扩展,它对所有注释执行此操作以使整个文档只读。 这是代码:

// A blank annotation that does nothing except serve to block user input
class BlockInputAnnotation: PDFAnnotation {

    init(forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
        super.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp,  withProperties: properties)
        self.fieldName = "blockInput"
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(with box: PDFDisplayBox, in context: CGContext)   {
    }
}


extension PDFDocument {
    func makeReadOnly() {
        for pageNumber in 0..<self.pageCount {
            guard let page = self.page(at: pageNumber) else {
                continue
            }
            for annotation in page.annotations {
                annotation.isReadOnly = true // This _should_ be enough, but PDFKit doesn't recognize the isReadOnly attribute
                // So we add a blank annotation on top of the annotation, and it will capture touch/mouse events
                let blockAnnotation = BlockInputAnnotation(forBounds: annotation.bounds, withProperties: nil)
                blockAnnotation.isReadOnly = true
                page.addAnnotation(blockAnnotation)
            }
        }

    }
}

我已经研究这个问题一段时间了,我终于找到了一些有效的方法。 对我来说,解决方案是使用一个.widget超过.freeText 此方法可防止文本在导出后被选中和修改。 我应该指出 PDF 并非万无一失,任何 PDF 都可以进行反编译,但对于日常上班族来说,这是一个完美的解决方案。

 //Don't use this - This is what most tutorials show
 //Which can be easily changed after export
 let annotation = PDFAnnotation(bounds: CGRect(x: 10 , y: 720 , width: 100, height: 50), forType: .freeText, withProperties: nil)

使用这个 - Swift 5

func addText(x: Int, y: Int, width: Int, height: Int, text: String, fontSize: CGFloat, centerText: Bool){
    
    //I'm using a PDFView but, if you are not displaying the PDF in the app then just use
    //let fileURL = Bundle.main.url(forResource: "MyPDF", withExtension: "pdf")!
    //let pdfDocument = PDFDocument(url: fileURL)
    //guard let document = pdfDocument else { return }

    guard let document = pdfView.document else { return }
    //I'm only using one page for my PDF but this is easy to change
    let lastPage = document.page(at: document.pageCount - 1)
    let annotation = PDFAnnotation(bounds: CGRect(x: x , y: y, width: width, height: height), forType: .widget, withProperties: nil)

    //Don't use contents and caption
    //annotation.contents = text
    //annotation.caption = text

    //Use this instead
    annotation.widgetFieldType = .text
    annotation.widgetStringValue = text

    //Check if the text should be centered
    if (centerText){ annotation.alignment = .center }

    //I'm using a custom font 
    annotation.font = UIFont(name: "calibri", size: fontSize)
    //You can use this instead
    //annotation.font = UIFont.systemFont(ofSize: fontSize)

    //Set the color
    annotation.fontColor = .black
    annotation.color = .clear
    annotation.backgroundColor = .clear

    //This is useless 
    annotation.isReadOnly = true

    //Add the annotation to the last page
    lastPage?.addAnnotation(annotation)
    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM