Как раскрасить аннотацию ссылки или добавить действие, чтобы выделить аннотацию с помощью PdfKit в Swift - PullRequest
1 голос
/ 07 марта 2019

У меня есть несколько PDF в моем приложении, и я использую PdfKit в Swift.Теперь я хочу выделить все гиперссылки в документе 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, упомянутого в коде.enter image description here

1 Ответ

1 голос
/ 11 марта 2019

Даже это не правильное решение, хотя это взломать.

Шаг 1. Создание аннотации ссылки и добавление существующего действия аннотации к новой аннотации ссылки.

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

Шаг 2. Добавьте новую аннотацию ссылки на страницу после выделенной аннотации.

page?.addAnnotation(linkAnnotation)

Попробуйте и дайте мне знать, если это работает для вас.

...