How do I create internal Anchor in iText?

The com.itextpdf.text.Anchor class in iText can be used to create an internal link or external link in a PDF document. To create an internal link we must format the anchor reference using the # + referenceName. On the other side the target anchor should be named using the same reference name excluding the # symbol.

To set the reference we use the setReference() method. To define the target anchor we can name the anchor using the setName() method.

package org.kodejava.example.itextpdf;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class AnchorInternalDemo {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("AnchorInternal.pdf"));
            document.open();

            Anchor anchor = new Anchor("[Continue Here]");
            anchor.setReference("#targetLink");
            Paragraph para1 = new Paragraph(
                    "The quick brown fox jumps over the lazy dog. ");
            para1.add(anchor);
            document.add(para1);

            Anchor target = new Anchor(
                    "The quick onyx goblin jumps over the lazy dwarf.");
            anchor.setName("targetLink");
            Paragraph para2 = new Paragraph();
            para2.setSpacingBefore(550);
            para2.add(target);
            document.add(para2);

            document.close();
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.9</version>
</dependency>

What is reference variable in Java?

The only way you can access an object is through a reference variable. A reference variable is declared to be of a specific type and that type can never be changed. Reference variables can be declared as static variables, instance variables, method parameters, or local variables.

A reference variable that is declared as final can’t never be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed.

package org.kodejava.example.basic;

public class ReferenceDemo {
    public static void main(String[] args) {
        // Declaration of Reference variable
        Reference ref1, ref2;

        // ref3 is declared final, ref3 can't be reassigned
        // or refer to different object
        final Reference ref3;

        // assign ref1 with object Reference
        ref1 = new Reference("This is the first reference variable", 1);

        // access method getNumber() of object Reference through
        // variable ref1
        int number = ref1.getNumber();
        System.out.println("number= " + number);

        // assign ref2 with object Reference
        ref2 = new Reference("This is the second reference variable", 2);

        // passing ref2 as method parameter of printText() method
        ReferenceDemo.printText(ref2);

        // assign ref3 with object Reference
        ref3 = new Reference("This is the third reference variable", 3);

        // try to reassign ref3 will cause a compile-time error
        // ref3 = new Reference("Try to reassign", 3);

    }

    public static void printText(Reference reference) {
        String text = reference.getText();
        System.out.println(text);
    }
}
package org.kodejava.example.basic;

public class Reference {
    private int number;
    private String text;

    Reference(String text, int number) {
        this.text = text;
        this.number = number;
    }

    public String getText() {
        return text;
    }

    public int getNumber() {
        return number;
    }
}