PDFKit PDFSelecction边界框坐标系

时间:2018-07-10 20:20:22

标签: ios swift xcode ios11

我想在pdf中找到一个特定的单词并在其上画一个边框。 为此,我使用

let searchResult = document.findString("VAT", withOptions: NSString.CompareOptions.caseInsensitive)

for result in searchResult {
    let box = result(for: document.page(at: 0)!)
    let convertedBox = pdf.convert(box, from: document.page(at: 0)!)
    let boxView = UIView(frame: convertedBox)
    boxView.backgroundColor = UIColor(red: 0.3, green: 0.6, blue: 0.1, alpha: 0.5)
    view.addSubview(boxView)
    print(box)
}

PDFSelection的坐标系是什么? 我想在pdfview的顶部绘制边界框(pdfselection的属性),并在一个尴尬的地方获取矩形。

有人知道如何转换此坐标吗?

这是我的代码

 {"products":[
  {
     "description":"E-Series Util",
     "effectiveEndDate":"2099-04-20",
     "effectiveStartDate":"2018-04-20",
     "id":"2c92c0f962e1d5af0162e698b14b6520",
     "name":"NOD-E-series-Utilities",
     "productFeatures":[

     ],
     "productRatePlans":[
        {
           "description":"",
           "effectiveEndDate":"2025-04-20",
           "effectiveStartDate":"2018-04-20",
           "id":"2c92c0f862e1caed0162e699ffc922e6",
           "name":"NOD-E series-Utilities",
           "productRatePlanCharges":[
              {
                 "billingDay":"SpecificDayofMonth/1st of the month",
                 "billingPeriod":"Month",
                 "billingPeriodAlignment":"AlignToCharge",
                 "description":"",
                 "endDateCondition":"Subscription_End",
                 "financeInformation":{
                    "deferredRevenueAccountingCode":"",
                    "recognizedRevenueAccountingCode":""
                 },
                 "id":"2c92c0f962e1d5af0162e6a313cc752d",
                 "model":"PerUnit",
                 "name":"NOD-E Series-Utilities-NR",
                 "pricing":[
                    {
                       "currency":"USD",
                       "price":"0E-9"
                    }
                 ],
                 "pricingSummary":[
                    "USD0/NR"
                 ],
                 "ratingGroup":"ByBillingPeriod",
                 "revenueRecognitionRuleName":"Recognize upon invoicing",
                 "taxCode":"",
                 "taxable":false,
                 "triggerEvent":"ContractEffective",
                 "type":"Usage",
                 "uom":"NR",
                 "usageRecordRatingOption":"EndOfBillingPeriod",
                 "useTenantDefaultForPriceChange":true
              },
              {
                 "billingDay":"SpecificDayofMonth/1st of the month",
                 "billingPeriod":"Month",
                 "billingPeriodAlignment":"AlignToCharge",
                 "description":"",
                 "endDateCondition":"Subscription_End",
                 "financeInformation":{
                    "deferredRevenueAccountingCode":"",
                    "recognizedRevenueAccountingCode":""
                 },
                 "id":"2c92c0f862e1caed0162e6a0cb842eab",
                 "model":"PerUnit",
                 "name":"NOD-E Series-Utilities-Usage",
                 "pricing":[
                    {
                       "currency":"USD",
                       "price":"0.100000000"
                    }
                 ],
                 "pricingSummary":[
                    "USD0.1/GB"
                 ],
                 "ratingGroup":"ByBillingPeriod",
                 "revenueRecognitionRuleName":"Recognize upon invoicing",
                 "taxCode":"",
                 "taxable":false,
                 "triggerEvent":"ContractEffective",
                 "type":"Usage",
                 "uom":"GB",
                 "usageRecordRatingOption":"EndOfBillingPeriod",
                 "useTenantDefaultForPriceChange":true
              }
           ],
           "status":"Active"
        }
     ],
     "sku":"SKU-00000020"
  }
  }

谢谢

1 个答案:

答案 0 :(得分:1)

PDFSelection是页面中找到匹配项的位置

此选择的范围是相对于页面本身的。

这是获取界限,然后使用其添加注释的示例

let searchResult = document.findString("VAT", withOptions: .caseInsensitive)

for result in searchResult {

    let bounds = result.pages.map { result.bounds(for: $0)) }

    let annotations = bounds.map { PDFAnnotation(bounds: $0, forType: .highlight, withProperties: nil) }

    annotations.forEach { $0.page?.addAnnotation($0) }
}

请注意,如果文本很长,则可能在两页或更多页面中找到匹配项

在上面的示例中,注释的颜色可以通过以下方式设置:PDFAnnotation.color属性

PDFView包含一个UIScrollView,您可以通过以下扩展名获得它:

extension PDFView {
    var scrollView: UIScrollView? {
        return subviews.first(where: { $0 is UIScrollView }) as? UIScrollView
    }
}

然后尝试通过添加前几页的大小来计算实际框架,然后将其作为要在滚动视图内部添加的框架