LinkAnnotation Class
Defines a link annotation.
Declaration
public class LinkAnnotation extends BaseAnnotation
Remarks
A link annotation defines a clickable region on a PDF page. When a user clicks the region, the annotation navigates to a destination or executes an action.
To add a link annotation to a page, do the following:
- Create a
LinkAnnotation. - Assign an action to the link annotation.
- Add the link annotation to the page’s annotation collection.
The following code snippet creates a link annotation that opens the DevExpress website:
import com.devexpress.docs.pdf.*;
import com.devexpress.system.drawing.*;
import java.io.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try (InputStream documentStream =
Files.newInputStream(Path.of("Document.pdf"));
PdfDocument pdfDocument = new PdfDocument(documentStream)) {
Page page = pdfDocument.getPages().getFirst();
// Add link text.
float textX = 50;
float textY = 800;
TextFragment textFragment = new TextFragment() {{
setText("Visit the DevExpress website");
setLocation(new PointF(textX, textY));
}};
page.getFragments().add(textFragment);
// Create a link annotation.
float textWidth = textFragment.measure().getSize().getWidth();
float textHeight = textFragment.measure().getSize().getHeight();
float offset = 10;
LinkAnnotation link = new LinkAnnotation(new RectangleF(
textX - offset,
textY - offset,
textWidth + offset * 2,
textHeight + offset * 2));
UriAction action = new UriAction();
action.setUri("https://www.devexpress.com");
link.setAction(action);
link.setHighlightingMode(AnnotationHighlightingMode.INVERT);
page.getAnnotations().add(link);
pdfDocument.save(Files.newOutputStream(Path.of("Result.pdf")));
}
}
}
Refer to the following help topic for additional information: Document Navigation Annotations.
Inherited Members
Inheritance
Constructors
LinkAnnotation(OrientedRectangle[] rectangles) Constructor
Initializes a new instance of the LinkAnnotation class with specified target regions.
Declaration
public LinkAnnotation(OrientedRectangle[] rectangles)
Parameters
| Name | Type | Description |
|---|---|---|
| rectangles | OrientedRectangle[] | An array of oriented rectangles that define the clickable regions of the link annotation. |
LinkAnnotation(RectangleF bounds) Constructor
Initializes a new instance of the LinkAnnotation class with specified bounds.
Declaration
public LinkAnnotation(RectangleF bounds)
Parameters
| Name | Type | Description |
|---|---|---|
| bounds | RectangleF | The annotation bounds. |
Methods
getAction() Method
Returns the link annotation action.
Declaration
public InteractiveAction getAction()
Returns
| Type | Description |
|---|---|
| InteractiveAction | The link annotation action. |
getBorderStyle() Method
Returns the link annotation border style.
Declaration
public AnnotationBorderStyle getBorderStyle()
Returns
| Type | Description |
|---|---|
| AnnotationBorderStyle | The link annotation border style. |
getDestination() Method
Returns the link annotation destination.
Declaration
public Destination getDestination()
Returns
| Type | Description |
|---|---|
| Destination | The link annotation destination. |
getHighlightingMode() Method
Returns the link annotation highlighting mode used when users press the link area.
Declaration
public AnnotationHighlightingMode getHighlightingMode()
Returns
| Type | Description |
|---|---|
| AnnotationHighlightingMode | The link annotation highlighting mode. |
getPage() Method
Returns the page that contains the link annotation.
Declaration
public Page getPage()
Returns
| Type | Description |
|---|---|
| Page | The page that contains the link annotation. |
getRegion() Method
Returns the link annotation region.
Declaration
public List<Quadrilateral> getRegion()
Returns
| Type | Description |
|---|---|
| java.util.List<Quadrilateral> | The link annotation region. |
setAction(InteractiveAction value) Method
Sets the link annotation action.
Declaration
public void setAction(InteractiveAction value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | InteractiveAction | The link annotation action. |
setBorderStyle(AnnotationBorderStyle value) Method
Sets the link annotation border style.
Declaration
public void setBorderStyle(AnnotationBorderStyle value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | AnnotationBorderStyle | The link annotation border style. |
setDestination(Destination value) Method
Sets the link annotation destination.
Declaration
public void setDestination(Destination value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | Destination | The link annotation destination. |
setHighlightingMode(AnnotationHighlightingMode value) Method
Sets the link annotation highlighting mode.
Declaration
public void setHighlightingMode(AnnotationHighlightingMode value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | AnnotationHighlightingMode | The link annotation highlighting mode. |
setPage(Page value) Method
Sets the page that contains the link annotation.
Declaration
public void setPage(Page value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | Page | The page that contains the link annotation. |
setRegion(List<Quadrilateral> value) Method
Sets the link annotation region.
Declaration
public void setRegion(List<Quadrilateral> value)
Parameters
| Name | Type | Description |
|---|---|---|
| value | java.util.List<Quadrilateral> | The link annotation region. |