链接注释
链接注释(Link)用于在 PDF 页面上创建可交互的超链接区域,点击后可跳转到 URI 地址或文档内的指定页面。链接注释不属于 Markup 注释,不支持透明度、回复等标记特性。
链接动作类型
链接注释通过关联不同的 Action 对象实现不同的跳转行为:
| 动作类型 | 类 | 说明 |
|---|---|---|
| URI 跳转 | FSURIAction | 打开外部 URL(如网页链接) |
| 页面跳转 | FSGotoAction | 跳转到文档内的指定页面和位置 |
示例:创建 URI 链接注释
objc
#import <FoxitRDK/FSPDFObjC.h>
FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:@"path/to/Sample.pdf"];
[doc load:nil];
FSPDFPage *page = [doc getPage:0];
// 创建链接注释
FSRectF *rect = [[FSRectF alloc] initWithLeft1:100 bottom1:700 right1:300 top1:720];
FSAnnot *annot = [page addAnnot:FSAnnotLink rect:rect];
FSLink *link = [[FSLink alloc] initWithAnnot:annot];
if (!link || [link isEmpty]) return;
// 创建 URI 动作并设置目标 URL
FSAction *action = [FSAction create:doc action_type:FSActionTypeURI];
FSURIAction *uriAction = [[FSURIAction alloc] initWithAction:action];
[uriAction setURI:@"https://www.foxitsoftware.com"];
// 将动作关联到链接注释
[link setAction:uriAction];
[link resetAppearanceStream];
[doc saveAs:@"path/to/output.pdf" saveFlags:FSPDFDocSaveFlagNormal];
示例:创建页面跳转链接
objc
#import <FoxitRDK/FSPDFObjC.h>
FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:@"path/to/Sample.pdf"];
[doc load:nil];
FSPDFPage *page = [doc getPage:0];
// 创建链接注释
FSRectF *rect = [[FSRectF alloc] initWithLeft1:100 bottom1:650 right1:250 top1:670];
FSAnnot *annot = [page addAnnot:FSAnnotLink rect:rect];
FSLink *link = [[FSLink alloc] initWithAnnot:annot];
// 创建目标位置(跳转到第 5 页,适合页面宽度显示)
FSDestination *dest = [FSDestination createFitPage:doc page_index:4];
// 创建 Goto 动作并关联目标
FSAction *action = [FSAction create:doc action_type:FSActionTypeGoto];
FSGotoAction *gotoAction = [[FSGotoAction alloc] initWithAction:action];
[gotoAction setDestination:dest];
[link setAction:gotoAction];
[link resetAppearanceStream];
备注
[FSDestination createFitPage:page_index:] 中的 page_index 为从 0 开始的页码索引。SDK 还提供 createXYZ:page_index:left:top:zoom:、createFitWidth:page_index:top: 等方法创建不同缩放模式的目标位置。
高亮模式
链接注释支持设置点击时的视觉反馈效果:
| 常量 | 说明 |
|---|---|
FSAnnotHighlightingNone | 无高亮效果 |
FSAnnotHighlightingInvert | 反转区域内容颜色(默认) |
FSAnnotHighlightingOutline | 反转边框颜色 |
FSAnnotHighlightingPush | 按下效果 |
objc
[link setHighlightingMode:FSAnnotHighlightingInvert];
核心方法汇总
| 方法 | 说明 |
|---|---|
getAction / setAction: | 获取/设置链接关联的动作 |
removeAction | 移除关联动作 |
getHighlightingMode / setHighlightingMode: | 获取/设置高亮模式 |