简体   繁体   English

UIGraphicsSetPDFContextURLForRect不会在Core Graphics PDF上创建可点击区域

[英]UIGraphicsSetPDFContextURLForRect does not create clickable region on a Core Graphics PDF

I am trying to create a rectangular region on my PDF that will go to a URL. 我正在尝试在PDF上创建一个矩形区域,该区域将转到URL。 The PDF is created in an iOS app using Core Graphics routines. PDF是使用Core Graphics例程在iOS应用中创建的。

In a search to find anyone with a similar problem, I found this Stack Overflow question: 在寻找任何有类似问题的人的过程中,我发现了这个Stack Overflow问题:

Embed hyperlink in PDF using Core Graphics on iOS 在iOS上使用Core Graphics将超链接嵌入PDF

If I use the code provided in the answer of the above question, it sort of works for me. 如果我使用上述问题的答案中提供的代码,那么它对我来说很有效。 The text shows up and is a clickable link, but if I specify a rectangle that is much larger than the text, only the text is clickable. 文本显示出来并且是可单击的链接,但是如果我指定一个比文本大得多的矩形,则只有文本是可单击的。 In addition, modified the code a bit so that I could provide a label and a URL separately, and when I did this and provided an empty string, there was nothing anywhere in the rectangle that was clickable. 另外,对代码进行了一些修改,以便我可以分别提供标签和URL,并且当我这样做并提供空字符串时,矩形中的任何地方都没有可单击的位置。

However, if I use the UIKit routines for creating a PDF (basically UIGraphicsBeginPDFContextToFile and descendants), then the call to UIGraphicsSetPDFContextURLForRect creates a region in the resulting PDF file that is clickable, even if I do not provide any text. 但是,如果我使用UIKit例程创建PDF(基本上是UIGraphicsBeginPDFContextToFile和其后代),则对UIGraphicsSetPDFContextURLForRect的调用会在生成的PDF文件中创建一个可单击的区域,即使我不提供任何文本也是如此。

Here is the method that I am using to create the rectangle link: 这是我用来创建矩形链接的方法:

+ (void)rectLink:(NSString *)urlString frame:(CGRect)frameRect cropBox:(CGRect)cropBoxRect sourceContext:(CGContextRef)context
{
    UIGraphicsPushContext(context);

#ifdef OUTPUT_TEXT_LINK_OUTLINE
    CGContextSetLineWidth(context, 5.0);
    CGContextSetStrokeColorWithColor(context, [UIColor purpleColor].CGColor);
    CGContextStrokeRect(context, frameRect);
#endif

    NSURL *url = [NSURL URLWithString:urlString];
    UIGraphicsSetPDFContextURLForRect(url, frameRect);

    UIGraphicsPopContext();
}

(The OUTPUT_TEXT_LINK_OUTLINE define is something that I am using for testing to make sure that the rectangle I am passing in is in fact where it is supposed to be.) (我正在使用OUTPUT_TEXT_LINK_OUTLINE定义进行测试,以确保所传递的矩形实际上位于应该位于的位置。)

And here is a call I am making to the above method on the first page of my PDF generation: 这是我在生成PDF的第一页上对上述方法的调用:

[self rectLink:@"http://stackoverflow.com" frame:CGRectMake(474,22,28,28) cropBox:cropBoxRect sourceContext:context];

The variable cropBoxRect refers to the crop box for the current page, and context is the value that I am saving from the CGPDFContextCreateWithURL call at the start of the document. 变量cropBoxRect引用当前页面的裁剪框,上下文是我从文档开头的CGPDFContextCreateWithURL调用中保存的值。

It is better to write your own writer for writing such annotations to pdf instead of using core graphics APIs, As Apple has not taken PDFKit to cocoa-touch, 最好编写您自己的编写器,以将此类注释写入pdf而不是使用核心图形API,因为Apple尚未将PDFKit用于可可触摸,
- You can follow simplest "incremental update" approach for modifying the pdf which is mentioned in PDF reference 1.7. -您可以按照最简单的“增量更新”方法来修改PDF参考1.7中提到的pdf。
- You need to search the page object number in pdf then get /Annots array in it add your annotation token, append its object number to /Annots array -您需要搜索pdf中的页面对象编号,然后在其中获取/ Annots数组,添加注释标记,然后将其对象编号附加到/ Annots数组
- Make entries for newly added/modified tokens in xref table properly. -在外部参照表中正确输入新添加/修改的令牌。
- Add trailer token.. -添加预告片令牌。
This is how you can add any types of annotations into pdf. 这是您可以将任何类型的注释添加到pdf中的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM