简体   繁体   中英

PDFKit - annotation not appearing on PDF

I'm trying to create a simple annotation (ie a line with arrowheads) on a pdf. The code compiles, the pdf is shown but no line is drawn.

import UIKit
import PDFKit

class ViewController: UIViewController, PDFViewDelegate {

var pdfView: PDFView!

override func viewDidLoad() {
    super.viewDidLoad()

    configureUI()
    loadPDF()
    configurePDFView()

}

private func configureUI() {

    pdfView = PDFView()
    pdfView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(pdfView)

    pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    pdfView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
private func loadPDF() {
    guard
        let url = Bundle.main.url(forResource: "RA", withExtension: "pdf"),
        let document = PDFDocument(url: url)
        else { fatalError() }
    pdfView.document = document
}

private func configurePDFView() {
    pdfView.delegate =  self
    let bounds = CGRect(x: 10, y:10 , width: 100, height: 100)

    let line = PDFAnnotation(bounds: bounds, forType: .line, withProperties: nil)
            line.setValue([0,0,100,100], forAnnotationKey: .linePoints)
            line.setValue(["Closed", "Open"], forAnnotationKey: .lineEndingStyles)
            line.setValue(UIColor.red, forAnnotationKey: .color)

            line.startPoint = CGPoint(x: 10, y: 10)
            line.endPoint = CGPoint(x: 100, y: 100)
            line.startLineStyle = .closedArrow
            line.endLineStyle = .openArrow
            line.color = .green

            let border = PDFBorder()
            border.lineWidth = 2.0
            line.border = border
       }
     }

Mystery to me this one, must be a simple fix. A missing line of code. Your assistance is appreciated.

I forgot to add the annotation to the page!

let page = pdfView.document?.page(at: 0)
    page?.addAnnotation(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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