1

我的应用程序中有一些 pdf,并且正在快速使用 PdfKit。现在我想突出显示 pdf 文档中的所有超链接。我尝试使用 PDFAnnotationSubtype.highlight 和 PDFAnnotationSubtype.link 但在这两种情况下我都无法实现我的目标。

对于 PDFAnnotationSubtype.highlight - 单击链接时,我无法对其添加操作。

对于 PDFAnnotationSubtype.link - 我无法为链接设置颜色或背景颜色。

这是我的代码,如果在这里遗漏了什么,请纠正我。

//here i get my pdf from document directory
let filePAth = (self.getDirectoryPath() as NSString).appendingPathComponent(webURLString)
    let fileURL = URL(fileURLWithPath: filePAth)

    let document = PDFDocument(url: fileURL)

    self.pdfView.displayMode = .twoUpContinuous
    self.pdfView.displayDirection = .horizontal
     pdfView.usePageViewController(true, withViewOptions: [UIPageViewController.OptionsKey.interPageSpacing: 0])

    //here is the hack 
    //i am getting annotation  
   let count = document?.pageCount

    for i in 0 ..< count! {

        let page = document?.page(at: i)
        let annotationArray = page1?.annotations

      for annotationObj in annotationArray! {
            if annotationObj.type! == "Link" {

                //case 1: highlights hyperlinks links but
                //added action does not work
                let annotation = PDFAnnotation(bounds: obj.bounds, forType: PDFAnnotationSubtype.highlight, withProperties: nil)

                annotation.color = UIColor.red
                annotation.isHighlighted = true

                // highlights all the hyperlinks with red color
                // but added action does not work here

                let url = URL(string: "http://google.com/")
                annotation.action = PDFActionURL(url: url!)

               page?.addAnnotation(annotation)

             //case 2: does not highlights hyperlinks links with red 
             //color but added action works

                let annotation = PDFAnnotation(bounds: obj.bounds, forType: PDFAnnotationSubtype.link, withProperties: nil)
                // no effect 
                annotation.color = UIColor.red
                annotation.isHighlighted = true

                // does not highlight all the hyperlinks with red 
                // but added action works here

                let url = URL(string: "http://google.com/")
                annotation.action = PDFActionURL(url: url!)

               page?.addAnnotation(annotation)

            }
        }
    }

我能找到路。任何人都可以提出一些建议吗?图像是代码中提到的案例 1 的结果。 在此处输入图像描述

4

2 回答 2

1

即使它不是一个适当的解决方案,尽管它是一个 hack。

第 1 步 - 创建链接注释并将现有注释操作添加到新链接注释。

let linkAnnotation = PDFAnnotation(bounds: obj.bounds, forType: PDFAnnotationSubtype.link, withProperties: nil)
linkAnnotation.action = obj.annotation

第 2 步 - 在突出显示的注释之后向页面添加新的链接注释。

page?.addAnnotation(linkAnnotation)

尝试让我知道这是否适合您。

于 2019-03-11T10:03:10.140 回答
0

您不需要为注释添加任何操作,您可以通过添加通知观察免费获得它,因为当单击注释时,PDFKit 将发布名为“PDFViewAnnotationHit”的通知。这是Apple的文档

func addAnnotationNotification() {
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(annotationHit(notification:)),
        name: Notification.Name.PDFViewAnnotationHit, object: nil
    )
}

@objc func annotationHit(notification: Notification) {
    if let annotation = notification.userInfo?["PDFAnnotationHit"] as? PDFAnnotation {
        print(annotation.bounds)
        print(annotation.contents!)
    }
}
于 2021-02-06T15:57:42.487 回答