swift PDFKit突出显示注解:四边形点

u2nhd7ah  于 2023-06-28  发布在  Swift
关注(0)|答案(3)|浏览(159)

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

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

但它只是突出显示一个矩形,我想突出显示多行文本。我找到了一个问题/答案Wrong highlight annotation on apple PDFKit
但它不是我想要的,它会添加许多突出显示注解,我只想添加一个注解。我了解到键值QuadPoints可以做到这一点。但是当我添加下面的代码时,它不起作用,甚至不能呈现注解。

  1. NSArray<NSValue *> *quadrilateralPoints = [[NSArray alloc] initWithObjects:
  2. [NSValue valueWithCGPoint:CGPointMake(206.0f, 659.0f)],
  3. [NSValue valueWithCGPoint:CGPointMake(266.0f, 659.0f)],
  4. [NSValue valueWithCGPoint:CGPointMake(206.0f, 600.0f)],
  5. [NSValue valueWithCGPoint:CGPointMake(266.0f, 600.0f)],
  6. nil];
  7. annotation.quadrilateralPoints = quadrilateralPoints;

现在我想知道如何实现它。或如何使用quadrilateralPoints?

eqzww0vc

eqzww0vc1#

我找到了答案:下面的代码工程

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

因为它基于原点边界

sigwle7e

sigwle7e2#

我发现更好的方法是使用.quadPoints annotationKey。此方法符合PDF规范,并且不是PDFAnnotationUtilities. h文件的一部分,该文件是为了兼容遗留函数而提供的。这种方式不需要您调整边界。

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

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

hmtdttj4

hmtdttj43#

我走上了同样的道路,但我遇到了更令人沮丧的事情。我似乎无法捕捉与触摸突出显示菜单元素的人相关的事件。见下图。我似乎无法捕捉到这一事件。我在10年前成功地完成了这一点,但不是用PDFView。

相关问题