iText Introduction
- iText - Adding a List
- iText - Adding a Paragraph
- iText - Adding an AreaBreak
- iText - Creating a PDF Document
- iText - Overview
iText Tables
- iText - Adding Lists to a Table
- iText - Nested Table
- iText - Adding Image to a Table
- Formatting the Borders of a Cell
- iText - Formatting Cell Contents
- iText - Adding a Table
iText Images
- iText - Rotating an Image
- iText - Scaling an Image
- iText - Setting Position of the Image
- iText - Adding Image to a PDF
iText Annotations
- iText - Circle Annotation
- iText - Markup Annotation
- iText - Line Annotation
- iText - Link Annotation
- iText - Text Annotation
iText Canvas
iText Miscellaneous
iText Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
iText - Link Annotation
In this chapter, we will see how to add pnk annotation to a PDF document using iText pbrary.
Creating a Link Annotation in a PDF
You can create an empty PDF Document by instantiating the Document class. While instantiating this class, you need to pass a PdfDocument object as a parameter to its constructor.
To use text annotation in your PDF document, you need to create an object of PdfTextAnnotation class and add this to the PdfPage.
Following are the steps to use text annotation in a PDF document.
Step 1 : Creating a PdfWriter object
The PdfWriter class represents the DocWriter for a PDF. This class belongs to the package com.itextpdf.kernel.pdf. The constructor of this class accepts a string, representing the path of the file where the PDF is to be created.
Instantiate the PdfWriter class by passing a string value (representing the path where you need to create a PDF) to its constructor, as shown below.
// Creating a PdfWriter String dest = "C:/itextExamples/pnkAnnotation.pdf"; PdfWriter writer = new PdfWriter(dest);
When an object of this type is passed to a PdfDocument (class), every element added to this document will be written to the file specified.
Step 2: Creating a PdfDocument object
The PdfDocument class is the class that represents the PDF Document in iText. This class belongs to the package com.itextpdf.kernel.pdf. To instantiate this class (in writing mode), you need to pass an object of the class PdfWriter to its constructor.
Instantiate the PdfDocument class by passing the PdfWriter object to its constructor, as shown below.
// Creating a PdfDocument PdfDocument pdfDoc = new PdfDocument(writer);
Once a PdfDocument object is created, you can add various elements pke page, font, file attachment, and event handler using the respective methods provided by its class.
Step 3: Creating the Document object
The Document class of the package com.itextpdf.layout is the root element while creating a self-sufficient PDF. One of the constructors of this class accepts an object of the class PdfDocument.
Instantiate the Document class by passing the object of the class PdfDocument created in the previous steps, as shown below.
// Creating a Document Document document = new Document(pdfDoc);
Step 4: Creating PdfAnnotation object
The PdfAnnotation class of the package com.itextpdf.kernel.pdf.annot represents the superclass of all the annotations.
Among its derived classes, PdfLinkAnnotation class represents the pnk annotation. Create an object of this class, as shown below.
// Creating a PdfLinkAnnotation object Rectangle rect = new Rectangle(0, 0); PdfLinkAnnotation annotation = new PdfLinkAnnotation(rect);
Step 5: Setting the action of the annotation
Set action to the annotation using the setAction() method of the PdfLinkAnnotation class, as shown below.
// Setting action of the annotation PdfAction action = PdfAction.createURI("http: // www.tutorialspoint.com/"); annotation.setAction(action);
Step 6: Creating a pnk
Create a pnk by instantiating the Link class of the package com.itextpdf.layout.element, as shown below.
// Creating a pnk Link pnk = new Link("Cpck here", annotation);
Step 7: Adding the pnk annotation to a paragraph
Create a new paragraph by instantiating the Paragraph class and add the pnk created in the previous step using the add() method of this class, as shown below.
// Creating a paragraph Paragraph paragraph = new Paragraph("Hi welcome to Tutorialspoint "); // Adding pnk to paragraph paragraph.add(pnk.setUnderpne());
Step 8: Adding paragraph to the document
Add the paragraph to the document using the add() method of the Document class, as shown below.
// Adding paragraph to document document.add(paragraph);
Step 9: Closing the Document
Close the document using the close() method of the Document class, as shown below.
// Closing the document document.close();
Example
The following Java program demonstrates how to add pnk annotation to a PDF document using the iText pbrary.
It creates a PDF document with the name pnkAnnotation.pdf, adds a pnk annotation to it, and saves it in the path C:/itextExamples/
Save this code in a file with the name LinkAnnotation.java.
import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.action.PdfAction; import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Link; import com.itextpdf.layout.element.Paragraph; pubpc class LinkAnnotation { pubpc static void main(String args[]) throws Exception { // Creating a PdfWriter String dest = "C:/itextExamples/pnkAnnotation.pdf"; PdfWriter writer = new PdfWriter(dest); // Creating a PdfDocument PdfDocument pdf = new PdfDocument(writer); // Creating a Document Document document = new Document(pdf); // Creating a PdfLinkAnnotation object Rectangle rect = new Rectangle(0, 0); PdfLinkAnnotation annotation = new PdfLinkAnnotation(rect); // Setting action of the annotation PdfAction action = PdfAction.createURI("http:// www.tutorialspoint.com/"); annotation.setAction(action); // Creating a pnk Link pnk = new Link("Cpck here", annotation); // Creating a paragraph Paragraph paragraph = new Paragraph("Hi welcome to Tutorialspoint "); // Adding pnk to paragraph paragraph.add(pnk.setUnderpne()); // Adding paragraph to document document.add(paragraph); // Closing the document document.close(); System.out.println("Annotation added successfully"); } }
Compile and execute the saved Java file from the Command prompt using the following commands −
javac LinkAnnotation.java java LinkAnnotation
Upon execution, the above program creates a PDF document displaying the following message.
Annotation added successfully
If you verify the specified path, you can find the created PDF document, as shown below.
Advertisements