PDFKit - 注释未出现在PDF上

时间:2017-10-19 09:28:00

标签: ios swift pdf

我正在尝试在pdf上创建一个简单的注释(即带箭头的线条)。代码编译,显示pdf但不绘制任何行。

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
       }
     }

对我来说这个谜,必须是一个简单的修复。缺少一行代码。非常感谢您的协助。

1 个答案:

答案 0 :(得分:1)

我忘了将注释添加到页面中!

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