Adding Link in PDF Document using iText Library – Java Example
This example shows how to add a link in PDF using iText in Java.
Program for adding a URL or hyperlink in PDF document using Apache iText library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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 document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream("AnchorDemo.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("javac - Java Examples Source Code"); anchor.setReference("http://javac.in"); paragraph.add(anchor); document.add(paragraph); } catch (DocumentException | FileNotFoundException e) { e.printStackTrace(); } finally { document.close(); } } } |