0

我对目标 c 和 Apple 的 PdfKit 框架还很陌生,我无法在我的 pdf 上绘制注释。

我在控制台上没有错误。这是我的代码:

PDFAnnotation  * observation = [[PDFAnnotation alloc] init];
CGRect cgRect = CGRectMake(20, 20, 120, 120);
                
observation.widgetFieldType = PDFAnnotationWidgetSubtypeButton;
observation.bounds = cgRect;
observation.shouldDisplay = true;
observation.backgroundColor = UIColor.redColor;
observation.widgetFieldType= PDFAnnotationWidgetSubtypeButton;
                
[page addAnnotation:observation];

有人知道为什么我的 pdfannotation 没有画在我的 pdf 上吗?我还想知道目标 c 是否完全支持 PdfKit 框架,因为苹果的文档中只有使用 swift 制作的示例。

谢谢您的帮助 !

4

1 回答 1

1

您的注释未绘制,因为您忘记设置type. 这可能是一个错误,因为您设置widgetFieldType了两次。这是正确的按钮小部件设置:

PDFAnnotation  *observation = [[PDFAnnotation alloc] init];
observation.bounds = CGRectMake(20, 20, 200, 100);

observation.type = PDFAnnotationSubtypeWidget;
observation.widgetFieldType = PDFAnnotationWidgetSubtypeButton;
observation.widgetControlType = kPDFWidgetCheckBoxControl;

observation.backgroundColor = UIColor.redColor;
[page addAnnotation:observation];

为避免将来出现类似的错误,请使用以下初始化程序:

- (instancetype)initWithBounds:(CGRect)bounds 
                       forType:(PDFAnnotationSubtype)annotationType 
                withProperties:(NSDictionary *)properties;

并将设置代码更改为:

PDFAnnotation  *observation = [[PDFAnnotation alloc] initWithBounds:CGRectMake(20, 20, 200, 100)
                                                            forType:PDFAnnotationSubtypeWidget
                                                     withProperties:nil];
observation.widgetFieldType = PDFAnnotationWidgetSubtypeButton;
observation.widgetControlType = kPDFWidgetCheckBoxControl;
observation.backgroundColor = UIColor.redColor;
    
[page addAnnotation:observation];

我强烈建议将外观(如backgroundColor)设置为最后一件事。因为当您更改类型时,所有这些值都会被 PDFKit 修改。

另请注意,它0, 0是左下角 ( bounds)。

于 2020-08-05T11:21:41.143 回答