链接注释
链接注释(Link)用于在 PDF 页面上创建可交互的超链接区域,点击后可跳转到 URI 地址或文档内的指定页面。链接注释不属于 Markup 注释,不支持透明度、回复等标记特性。
链接动作类型
链接注释通过关联不同的 Action 对象实现不同的跳转行为:
| 动作类型 | 类 | 说明 |
|---|---|---|
| URI 跳转 | URIAction | 打开外部 URL(如网页链接) |
| 页面跳转 | GotoAction | 跳转到文档内的指定页面和位置 |
示例:创建 URI 链接注释
java
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.actions.Action;
import com.foxit.sdk.pdf.actions.URIAction;
import com.foxit.sdk.pdf.annots.Annot;
import com.foxit.sdk.pdf.annots.Link;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
PDFPage page = doc.getPage(0);
// 创建链接注释
RectF rect = new RectF(100, 700, 300, 720);
Link link = new Link(page.addAnnot(Annot.e_Link, rect));
if (link == null || link.isEmpty()) return;
// 创建 URI 动作并设置目标 URL
URIAction uriAction = new URIAction(Action.create(doc, Action.e_TypeURI));
uriAction.setURI("https://www.foxitsoftware.com");
// 将动作关联到链接注释
link.setAction(uriAction);
link.resetAppearanceStream();
doc.saveAs("path/to/output.pdf", PDFDoc.e_SaveFlagNormal);
示例:创建页面跳转链接
java
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.Destination;
import com.foxit.sdk.pdf.actions.Action;
import com.foxit.sdk.pdf.actions.GotoAction;
import com.foxit.sdk.pdf.annots.Annot;
import com.foxit.sdk.pdf.annots.Link;
PDFDoc doc = new PDFDoc("path/to/Sample.pdf");
doc.load(null);
PDFPage page = doc.getPage(0);
// 创建链接注释
RectF rect = new RectF(100, 650, 250, 670);
Link link = new Link(page.addAnnot(Annot.e_Link, rect));
// 创建目标位置(跳转到第 5 页,适合页面宽度显示)
Destination dest = Destination.createFitPage(doc, 4);
// 创建 Goto 动作并关联目标
GotoAction gotoAction = new GotoAction(Action.create(doc, Action.e_TypeGoto));
gotoAction.setDestination(dest);
link.setAction(gotoAction);
link.resetAppearanceStream();
备注
Destination.createFitPage(doc, pageIndex) 中的 pageIndex 为从 0 开始的页码索引。SDK 还提供 createXYZ()、createFitWidth() 等方法创建不同缩放模式的目标位置。
高亮模式
链接注释支持设置点击时的视觉反馈效果:
| 常量 | 说明 |
|---|---|
Annot.e_HighlightingNone | 无高亮效果 |
Annot.e_HighlightingInvert | 反转区域内容颜色(默认) |
Annot.e_HighlightingOutline | 反转边框颜色 |
Annot.e_HighlightingPush | 按下效果 |
java
link.setHighlightingMode(Annot.e_HighlightingInvert);
核心方法汇总
| 方法 | 说明 |
|---|---|
getAction() / setAction(Action) | 获取/设置链接关联的动作 |
removeAction() | 移除关联动作 |
getHighlightingMode() / setHighlightingMode(int) | 获取/设置高亮模式 |