PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
J2SE J2EE Windows Linux Mac (OS X)

How To Create and Modify Links (Link Annotations) In A PDF Document

One of our customers wrote: “I want to inspect each link annotation, find out its type and destination and edit only those annotations pointing to a remote document.”
By Santhanam L.

In this article, you will learn how to create and edit actions for PDF link annotations. Action elements specify what happens when a user clicks on a link annotation in a PDF file.

There are several types of PDF actions - go-to, remote go-to, URI, JavaScript, launch, and named.

For this article, I will limit myself to go-to and remote go-to actions. A go-to action links to a destination in the same document while a remote go-to action links to a destination in another PDF document.

Our customer had a PDF document containing several link annotations. In particular, some link annotations had remote go-to actions - all of which were set to destinations in another PDF document.

A problem arose when the second PDF document was moved to a different location and all the remote go-to actions of link annotations in the original PDF document needed to be updated with the new path of the second document.

To reproduce the problem, let us create a PDF document (C:\Old_Path\document1.pdf) with two link annotations. A go-to action will be associated with the first link annotation and a remote go-to action will be associated with the second link annotation. The remote go-to action will be linked to a destination in another PDF document (C:\Old_Path\document2.pdf).

void createLinkAnnotActions() throws IOException, PdfException {
  // Create a new PDF document
  PdfDocument doc1 = new PdfDocument();
  // Create font instances
  PdfFont fontHelvetica = 
  PdfFont fontHelveticaSmall = 

  // Create new page 1
  PdfPage tempPage1 = new PdfPage(PdfPageSize.A4);

  // Write text on page 1 using the fonts
    "Create and Modify PDF Annotation Actions",
    50, 50);
    "Link to page 2 of this document.",
    50, 100);
    "Link to page 3 of remote document.",
    50, 120);

  // Create link annotation 1 on the first text element
  PdfLinkAnnot linkAnnot1 = 
    new PdfLinkAnnot(new PdfRect(81, 100, 33, 14),
  // Add a go-to action to link annotation 1
  linkAnnot1.addAction(new PdfGotoAction(2, 0, 0, 100));

  // Create link annotation 2 on the second text element
  PdfLinkAnnot linkAnnot2 =
    new PdfLinkAnnot(new PdfRect(81, 120, 33, 14),
  // Add a remote go-to action to link annotation 2
    new PdfRemoteGotoAction("C:\\Old_Path\\document2.pdf", 3, true));

  // Add the link annotations to page 1

  // Add page 1 to document

  // Create page 2
  tempPage1 = new PdfPage(PdfPageSize.A4);
  // Write some text on page 2
  tempPage1.writeText("Page 2", 50, 50);
  // Add page 2 to the document

  // Save document to file

Now, we assume that document2.pdf was moved from C:\Old_Path to D:\New_Path and update remote go-to actions for the link annotations in document1.pdf accordingly.

For this, we first load document1.pdf, retrieve its first page, iterate through all link annotations in that page, get all actions for each link annotation that is found, and update any remote go-to action with the new location of document2.pdf.

void manageLinkAnnotActions() throws IOException, PdfException {
  // Load document in which links need to be modified
  PdfDocument doc2 = new PdfDocument();
  String sExistingPath = "C:\\Old_Path";
  String sReplacementPath = "D:\\New_Path";
  // Access page 1
  PdfPage tempPage2 = doc2.getPage(1);
  // Obtain all link annotations in the page 
  List annotsList = tempPage2.getAllAnnotations(PdfAnnot.ANNOT_TYPE_LINK);
  // Iterate through the obtained annotations
  for (int i = 0, n = annotsList.size(); i < n; i++) {
    PdfLinkAnnot linkAnnot = (PdfLinkAnnot) annotsList.get(i);
    // Retrieve all remote go-to actions of 
    // the current link annotation
    List actionsList = linkAnnot.getAllActions(PdfAction.REMOTE_GOTO);
    // Iterate through the remote go-to 
    // actions of the current link annotation
    for (int j = 0, o = actionsList.size(); j < o; j++) {
      PdfRemoteGotoAction remoteGoToAction
         = (PdfRemoteGotoAction) actionsList.get(j);

      // Check for old pathname in the file pathname 
      // of the remote go-to action
      if (remoteGoToAction.getPdfFilePath().startsWith(sExistingPath)) {
        // Replace old pathname with new pathname
          "C:\\\\Old_Path",  // regex
  // Save modified document to file     

We use the getAllAnnotations() method of the page object to retrieve all link annotations objects in the page. We then call getAllActions() of each annotation and get a list of remote go-to action objects. The getPdfFilePath() is used to retrieve old pathname of document2.pdf. Finally, we use setPdfFilePath() to update the remote go-to action with the new pathname of document2.pdf.


Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.


A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2019 Gnostice Information Technologies Private Limited. All rights reserved.