【问题标题】:How to remove a annotation from a pdf file如何从pdf文件中删除注释
【发布时间】:2019-12-06 10:31:14
【问题描述】:

我正在添加注释,但无法删除添加的注释。 例如:当我搜索“Tubes”这个词时,它会标记每个“t”字母。

示例图片:

我只想注释“管”字。

我怎样才能删除我添加的所有注释。 我该如何解决这些问题?

代码如下:

var pdfView: PDFView!

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    return highlight(searchTerms: [searchText])
}

public func highlight(searchTerms: [String]?)
{
    var highlight = PDFAnnotation()
   searchTerms?.forEach { term in
    let selections = pdfView?.document?.findString(term, withOptions: [.forcedOrdering])
      selections?.forEach { selection in
         selection.pages.forEach { page in
            highlight = PDFAnnotation(bounds: selection.bounds(for: page), forType: .highlight, withProperties: nil)
            highlight.endLineStyle = .square
            highlight.color = UIColor.yellow.withAlphaComponent(0.5)
            page.addAnnotation(highlight)
         }
      }
   }
}

感谢大家的帮助:)

【问题讨论】:

    标签: ios swift uisearchbar ios-pdfkit


    【解决方案1】:

    您可以定义一个删除所有注释的函数,如下所示,并在searchBartextDidChange 方法中调用它:

    func removeAllAnnotations() {
        guard let document = pdfView.document else { return }
    
        for index in 0..<document.pageCount {
            if let page = document.page(at: index) {
                let annotations = page.annotations
                for annotation in annotations {
                    page.removeAnnotation(annotation)
                }
            }
        }
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        removeAllAnnotations()
        return highlight(searchTerms: [searchText])
    }
    
    

    【讨论】:

      猜你喜欢
      • 2015-12-20
      • 2021-02-20
      • 1970-01-01
      • 2015-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      相关资源
      最近更新 更多