【问题标题】:How to add anotation to my pdf using objective c?如何使用objective c向我的pdf添加注释?
【发布时间】:2020-11-25 12:04:06
【问题描述】:

我对 Objective 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];

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

感谢您的帮助!

【问题讨论】:

    标签: objective-c ios-pdfkit


    【解决方案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)。

    【讨论】:

    • 谢谢!不知道有没有办法画图代替按钮作为注解?
    • IIRC 你不能将它添加为注释,但你可以绘制它(有一些缺点) - 例如参见Inserting an Image into a PDF。它在 Swift 中,但您可以轻松地将其重写为 Obj-C。
    • 啊。我想在我的 PDF 上放置可点击的指针,你知道我是否可以代替绘制可点击的图片绘制自定义的可点击注释?谢谢
    猜你喜欢
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多