如何在 swift 中使用 PdfKit 为 link 注释着色或添加操作以突出显示注释
How to colour link annotation or add action to highlight annotation using PdfKit in swift
我的应用程序中有一些 pdf,并且正在 swift 中使用 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 的结果。
即使它不是一个合适的解决方案,尽管它是一个 hack。
第 1 步 - 创建 link 注释并将现有注释操作添加到新的 link 注释。
let linkAnnotation = PDFAnnotation(bounds: obj.bounds, forType: PDFAnnotationSubtype.link, withProperties: nil)
linkAnnotation.action = obj.annotation
第 2 步 - 在突出显示的注释后向页面添加新的 link 注释。
page?.addAnnotation(linkAnnotation)
请尝试告诉我这是否适合您。
您不需要为注释添加任何操作,您可以通过添加通知观察来免费获得它,因为 PDFKit 将在单击注释时 post 名为 'PDFViewAnnotationHit' 的通知。 Here is Apple's documentation
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!)
}
}
我的应用程序中有一些 pdf,并且正在 swift 中使用 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 的结果。
即使它不是一个合适的解决方案,尽管它是一个 hack。
第 1 步 - 创建 link 注释并将现有注释操作添加到新的 link 注释。
let linkAnnotation = PDFAnnotation(bounds: obj.bounds, forType: PDFAnnotationSubtype.link, withProperties: nil)
linkAnnotation.action = obj.annotation
第 2 步 - 在突出显示的注释后向页面添加新的 link 注释。
page?.addAnnotation(linkAnnotation)
请尝试告诉我这是否适合您。
您不需要为注释添加任何操作,您可以通过添加通知观察来免费获得它,因为 PDFKit 将在单击注释时 post 名为 'PDFViewAnnotationHit' 的通知。 Here is Apple's documentation
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!)
}
}