【问题标题】:PDFKit Highlight Annotation: quadrilateralPointsPDFKit 高亮注释:quadrilateralPoints
【发布时间】:2018-08-06 15:45:09
【问题描述】:

我想使用 PDFKit 在 pdf 文件中添加高亮注释。我使用下面的代码来添加它。

PDFPage* page = [self.pdfView.document pageAtIndex:0];
PDFAnnotation* annotation = [[PDFAnnotation alloc] initWithBounds:CGRectMake(206, 600, 60, 59) forType:PDFAnnotationSubtypeHighlight withProperties:nil];
annotation.color = UIColor.blueColor;
[page addAnnotation:annotation];

但它只是突出显示一个矩形,我想突出显示多行文本。我找到了一个问题/答案Wrong highlight annotation on apple PDFKit

但这不是我想要的,它会添加很多高亮注释,我只想添加一个注释。我了解到键值 QuadPoints 可以做到这一点。但是当我添加下面的代码时它不起作用,甚至无法渲染注释。

NSArray<NSValue *> *quadrilateralPoints = [[NSArray alloc] initWithObjects:
                                           [NSValue valueWithCGPoint:CGPointMake(206.0f, 659.0f)],
                                           [NSValue valueWithCGPoint:CGPointMake(266.0f, 659.0f)],
                                           [NSValue valueWithCGPoint:CGPointMake(206.0f, 600.0f)],
                                           [NSValue valueWithCGPoint:CGPointMake(266.0f, 600.0f)],
                                           nil];

annotation.quadrilateralPoints = quadrilateralPoints;

所以现在我想知道如何实现它?或者如何使用 quadrilateralPoints ?

【问题讨论】:

    标签: ios objective-c swift pdf pdfkit


    【解决方案1】:

    我找到了答案: 下面的代码有效

    NSArray<NSValue *> *quadrilateralPoints = [[NSArray alloc] initWithObjects:
                                           [NSValue valueWithCGPoint:CGPointMake(206.0 - 206, 659.0 - 600)],
                                           [NSValue valueWithCGPoint:CGPointMake(266.0 - 206, 659.0 - 600)],
                                           [NSValue valueWithCGPoint:CGPointMake(206.0 - 206, 600.0 - 600)],
                                           [NSValue valueWithCGPoint:CGPointMake(266.0 - 206, 600.0 - 600)],
                                           nil];
    
    annotation.quadrilateralPoints = quadrilateralPoints;
    

    因为它基于原点边界

    【讨论】:

      【解决方案2】:

      我发现一个更好的方法是使用 .quadPoints annotationKey。此方法与 PDF 规范匹配,并且不属于 PDFAnnotationUtilities.h 文件的一部分,该文件是为兼容旧版功能而提供的。这种方式不需要您调整边界。

      let quadPoints:[CGPoint] = [CGPoint(x:206.0, y:659.0),
                                  CGPoint(x:266.0, y:659.0),
                                  CGPoint(x:206.0, y:600.0),
                                  CGPoint(x:266.0, y:600.0)]
      
      
      annotation.setValue(quadPoints, forAnnotationKey: .quadPoints)
      
      

      要完成答案,您还应该使用 PDFSelection 的 selectionsByLine 函数来获取特定页面上的所有选择,并按照Wrong highlight annotation on apple PDFKit 中的说明为每页添加一个注释

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-25
        • 1970-01-01
        • 2018-04-06
        • 2015-01-21
        • 1970-01-01
        • 1970-01-01
        • 2015-04-13
        • 2016-05-19
        相关资源
        最近更新 更多