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.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 AnchorDemo {
public static void main(String[] args) {
Document doc = new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream("AnchorDemo.pdf"));
doc.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 by Examples");
anchor.setReference("https://kodejava.org");
paragraph.add(anchor);
doc.add(paragraph);
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
doc.close();
}
}
}
Maven Dependencies
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024
Provide some examples for internal links to go to specific chapter in pdf.
Hi Swaran, is the following example help? https://kodejava.org/how-do-i-create-internal-anchor-in-itext/