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>

How do I create Anchor or link in iText?

This example show you how to use the com.itextpdf.text.Anchor class to create an external link in the PDF document. You can create an instance of Anchor with a phrase of string and set its reference, in this case a website URL, by calling the setReference() method of this object.

package org.kodejava.example.itextpdf;

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

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

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

            String content = "You can learn Java programming on the " +
                    "following website: ";
            Paragraph paragraph = new Paragraph(content);

            // Creates a new anchor that link to external website
            // and add this anchor to the paragraph.
            Anchor anchor = new Anchor("Learn Java Programming by Examples");
            anchor.setReference("http://www.kodejava.org");
            paragraph.add(anchor);

            document.add(paragraph);
        } 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>