如何解决将带有正则表达式的注释添加到pdf页面时的快速问题
我想在pdf中搜索正则表达式,并使用正则表达式的结果根据其添加注释。我建立了一个简单的功能来做到这一点。正如一个了不起的社区(真的很努力的人,他们花了很多时间帮助我)发贴说,我可以使用 decomposedStringWithCompatibilityMapping 在pdf中正确搜索所需的表达式,但是之后我执行pdf选择找到界限,我遇到了不同。我发给我我的代码和一些图片。
func performRegex(regex:String,on pdfPage:PDFPage) {
guard let pdfString = pdfPage.string?.precomposedStringWithCanonicalMapping else { return }
guard let safeRegex = try? NSRegularExpression(pattern: regex,options: .caseInsensitive) else { return }
let results = safeRegex.matches(in: pdfString,options: .withoutAnchoringBounds,range: NSRange(pdfString.startIndex...,in: pdfString))
pdfPage.annotations.forEach { pdfPage.removeAnnotation($0)}
results.forEach { result in
let bbox = pdfPage.selection(for: result.range)?.bounds(for: pdfPage)
let annotation = PDFAnnotation(bounds: bbox!,forType: .highlight,withProperties: nil)
annotation.color = .yellow
annotation.contents = String(pdfString[Range(result.range,in:pdfString)!])
pdfPage.addAnnotation(annotation)
}
}
问题是当我执行此操作并输入此表达式[0-9]时,我的所有结果都发生了移位:
虽然我不使用precomposedStringWithCanonicalMapping,但是我的所有结果都不会移位,但是当我得到一个特殊字符时会遇到错误。
问题(我怀疑)在这行代码中。
let bbox = pdfPage.selection(for: result.range)?.bounds(for: pdfPage)
但是我不知道有什么工作要做。
请任何人能给我一些帮助!
非常感谢
解决方法
我现在能想到的唯一替代方法是使用原始字符串并修复格式错误的范围。尝试这样:
{
"$schema": "http://json.schemastore.org/proxies","proxies": {
"RequireContentType": {
"matchCondition": {
"route": "/api/HttpTrigger"
},"backendUri": "https://proxy-example.azurewebsites.net/api/HttpTrigger","requestOverrides": {
"backend.request.headers.content-type": "application/octet-stream","backend.request.headers.request-content-type": "'{request.headers.content-type}'"
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。