我的应用程序中有一些 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)
}
}
}