如何使用 PDFKit 突出显示 pdf 中的选定文本?

Posted

技术标签:

【中文标题】如何使用 PDFKit 突出显示 pdf 中的选定文本?【英文标题】:How to highlight selected text in pdf using PDFKit? 【发布时间】:2019-11-13 14:08:36 【问题描述】:

我已经设置了一个 PDFViewer,并想添加一个突出显示功能,以便当用户选择文本时,他们可以突出显示它。当您在笔记、iMessages 等中突出显示文本时,您可以选择全选、复制、粘贴等。您将如何编辑它以便同时拥有突出显示功能?此外,应用程序如何保存突出显示,以便当用户关闭并重新打开应用程序时,他们仍然能够查看突出显示的文本?这会涉及使用核心数据还是其他东西?谢谢!

this is a screenshot of the default functionalities that Apple provides but I would like to add an additional highlighting functionality

【问题讨论】:

【参考方案1】:
let select = pdfView.currentSelection?.selectionsByLine()
    //assuming for single-page pdf.
    guard let page = select?.first?.pages.first else  return 

    select?.forEach( selection in
        let highlight = PDFAnnotation(bounds: select.bounds(for: page), forType: .highlight, withProperties: nil)
        highlight.endLineStyle = .square
        highlight.color = UIColor.orange.withAlphaComponent(0.5)

        page.addAnnotation(highlight)
    )

【讨论】:

这应该在哪里调用?是否有选择文本时调用的委托方法? 这将有助于作为参考:- pdftron.com/blog/ios/…

以上是关于如何使用 PDFKit 突出显示 pdf 中的选定文本?的主要内容,如果未能解决你的问题,请参考以下文章

如何突出显示 *ngFor 中的选定行?

突出显示“ListFragment”中的选定项目?

如何突出显示 Vuetify 菜单中的选定项目?

使用 PDFKit 编辑 PDF

如何防止 PDFKit/NSImage 中的渲染伪影?

从 html 转换的 pdf 中的页码 - pdfkit、python/django