Link Annotations
Link annotations create interactive hyperlink regions on PDF pages. Taps can open a URI or jump to a page in the document. Link annotations are not Markup annotations and do not support opacity, replies, or other markup features.
Link Action Types
Link annotations use different Action objects:
| Action Type | Class | Description |
|---|---|---|
| URI | URIAction | Open an external URL |
| Go to page | GotoAction | Jump to a page and position in the document |
Example: URI Link Annotation
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;
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);Example: Go-To Page Link
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));
Destination dest = Destination.createFitPage(doc, 4);
GotoAction gotoAction = new GotoAction(Action.create(doc, Action.e_TypeGoto));
gotoAction.setDestination(dest);
link.setAction(gotoAction);
link.resetAppearanceStream();Note
pageIndex in Destination.createFitPage(doc, pageIndex) is 0-based. The SDK also provides createXYZ(), createFitWidth(), and other methods for different zoom modes.
Highlighting Mode
Visual feedback when the link is activated:
| Constant | Description |
|---|---|
Annot.e_HighlightingNone | No highlight |
Annot.e_HighlightingInvert | Invert region (default) |
Annot.e_HighlightingOutline | Invert border |
Annot.e_HighlightingPush | Push effect |
java
link.setHighlightingMode(Annot.e_HighlightingInvert);Core Methods
| Method | Description |
|---|---|
getAction() / setAction(Action) | Get/set associated action |
removeAction() | Remove action |
getHighlightingMode() / setHighlightingMode(int) | Get/set highlight mode |