Class LinkAnnotation


  • public class LinkAnnotation
    extends Object
    Defines a clickable link annotation that navigates to a specific page within the same PDF document. Useful for building custom tables of contents, cross-references, and in-document navigation.

    Mirrors IronPdf.Annotations.LinkAnnotation on the C# side.

    
     // Add a link on page 0 that navigates to page 5:
     LinkAnnotation link = new LinkAnnotation(0, 5);
     link.setX(50);
     link.setY(700);
     link.setWidth(200);
     link.setHeight(20);
     pdf.getAnnotation().addLinkAnnotation(link);
    
     // Custom TOC link with contents text:
     LinkAnnotation tocLink = new LinkAnnotation(0, 3, "Chapter 1 - Introduction");
     tocLink.setX(72);
     tocLink.setY(600);
     tocLink.setWidth(300);
     tocLink.setHeight(16);
     pdf.getAnnotation().addLinkAnnotation(tocLink);
     

    The link annotation creates a clickable region on the page. By default the region is invisible — set setShowBorder(boolean) to true for a visible border.

    Coordinate note: setY(int) sets the bottom edge of the clickable area using PDF coordinates (origin at bottom-left of page).

    • Constructor Detail

      • LinkAnnotation

        public LinkAnnotation​(int pageIndex,
                              int destinationPageIndex)
        Creates a link annotation on the specified page that navigates to a destination page.
        Parameters:
        pageIndex - Zero-based index of the page where the link is placed.
        destinationPageIndex - Zero-based index of the page to navigate to when clicked.
      • LinkAnnotation

        public LinkAnnotation​(int pageIndex,
                              int destinationPageIndex,
                              String contents)
        Creates a link annotation on the specified page that navigates to a destination page, with descriptive text for identification.
        Parameters:
        pageIndex - Zero-based index of the page where the link is placed.
        destinationPageIndex - Zero-based index of the page to navigate to when clicked.
        contents - Descriptive text for the link annotation.
    • Method Detail

      • getPageIndex

        public int getPageIndex()
        Returns:
        zero-based page index where this link annotation is placed
      • setPageIndex

        public void setPageIndex​(int pageIndex)
        Parameters:
        pageIndex - zero-based page index where this link annotation is placed
      • getDestinationPageIndex

        public int getDestinationPageIndex()
        Returns:
        zero-based page index this link navigates to when clicked
      • setDestinationPageIndex

        public void setDestinationPageIndex​(int destinationPageIndex)
        Parameters:
        destinationPageIndex - zero-based page index this link navigates to when clicked
      • getDestinationType

        public BookmarkDestinations getDestinationType()
        Returns:
        the destination type controlling how the target page is displayed
      • setDestinationType

        public void setDestinationType​(BookmarkDestinations destinationType)
        Parameters:
        destinationType - destination type controlling how the target page is displayed
      • getDestinationLeft

        public int getDestinationLeft()
      • setDestinationLeft

        public void setDestinationLeft​(int destinationLeft)
      • getDestinationRight

        public int getDestinationRight()
      • setDestinationRight

        public void setDestinationRight​(int destinationRight)
      • getDestinationTop

        public int getDestinationTop()
      • setDestinationTop

        public void setDestinationTop​(int destinationTop)
      • getDestinationBottom

        public int getDestinationBottom()
      • setDestinationBottom

        public void setDestinationBottom​(int destinationBottom)
      • getDestinationZoom

        public int getDestinationZoom()
      • setDestinationZoom

        public void setDestinationZoom​(int destinationZoom)
      • getAnnotationIndex

        public int getAnnotationIndex()
        Returns:
        annotation index on its page, or -1 if not yet added
      • setAnnotationIndex

        public void setAnnotationIndex​(int annotationIndex)
      • getX

        public int getX()
        Returns:
        horizontal position from the LEFT edge of the page, in points, or -1 if not set
      • setX

        public void setX​(int x)
      • getY

        public int getY()
        Returns:
        vertical position from the BOTTOM edge of the page, in points, or -1 if not set
      • setY

        public void setY​(int y)
      • getWidth

        public int getWidth()
      • setWidth

        public void setWidth​(int width)
      • getHeight

        public int getHeight()
      • setHeight

        public void setHeight​(int height)
      • getRectangle

        public Rectangle getRectangle()
        Returns:
        the clickable area of the link (may be null if not set)
      • setRectangle

        public void setRectangle​(Rectangle rectangle)
      • getColorCode

        public String getColorCode()
        Returns:
        color code for the link border (CSS #RRGGBB format) or null
      • setColorCode

        public void setColorCode​(String colorCode)
        Parameters:
        colorCode - color code for the link border (CSS #RRGGBB format) or null
      • isHidden

        public boolean isHidden()
        Returns:
        whether the link annotation is hidden from users
      • setHidden

        public void setHidden​(boolean hidden)
      • getContents

        public String getContents()
        Returns:
        descriptive text content for this link annotation
      • setContents

        public void setContents​(String contents)
      • getTitle

        public String getTitle()
        Returns:
        title of the link annotation
      • setTitle

        public void setTitle​(String title)
      • isShowBorder

        public boolean isShowBorder()
        Returns:
        whether the link annotation displays a visible border (default false)
      • setShowBorder

        public void setShowBorder​(boolean showBorder)
      • toString

        public String toString()
        Overrides:
        toString in class Object
        Returns:
        a string representation of this link annotation, e.g. "Link on page 2 -> page 5: My Link"