iText - Line Annotation


Advertisements

In this chapter, we will see how to add line annotation to a PDF document using iText library.

Creating a Line 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 the 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 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/lineAnnotation.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 PDFDocument 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 like page, font, file attachment, 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 is the superclass of all the annotations.

Among its derived classes, PdfLineAnnotation class represents the line annotation. Create an object of this class as shown below.

// Creating PdfAnnotation 
Rectangle rect = new Rectangle(20, 800, 0, 0); 
PdfAnnotation annotation = new PdfLineAnnotation(rect); 

Step 5: Setting the color of the annotation

Set color to the annotation using the setColor() method of the PdfAnnotation class. To this method, pass the color object representing the color of the annotation as a parameter.

// Setting color to the annotation 
annotation.setColor(Color.BLUE); 

Step 6: Setting the title and contents of the annotation

Set the title and contents of the annotation using the setTitle() and setContents() methods of the PdfAnnotation class respectively, as shown below.

// Setting title to the PdfLineAnnotation 
annotation.setTitle(new PdfString("iText"));        

// Setting contents of the PdfLineAnnotation 
annotation.setContents("Hi welcome to Howcodex"); 

Step 7: Adding the annotation to a page

Create a new PdfPage class using the addNewPage() method of the PdfDocument class and add the above created annotation using the addAnnotation() method of PdfPage class, as shown below.

// Creating a new page 
PdfPage page = pdf.addNewPage();

// Adding annotation to a page in a PDF 
page.addAnnotation(annotation); 

Step 8: 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 line annotation to a PDF document using the iText library. It creates a PDF document with the name lineAnnotation.pdf, adds a line annotation to it, and saves it in the path C:/itextExamples/.

Save this code in a file with name LineAnnotation.java.

import com.itextpdf.kernel.color.Color; 
import com.itextpdf.kernel.geom.Rectangle; 
import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfPage; 
import com.itextpdf.kernel.pdf.PdfString; 
import com.itextpdf.kernel.pdf.PdfWriter; 
import com.itextpdf.kernel.pdf.annot.PdfAnnotation; 
import com.itextpdf.kernel.pdf.annot.PdfLineAnnotation; 
import com.itextpdf.layout.Document;  

public class LineAnnotation {
   public static void main(String args[]) throws Exception {             
      // Creating a PdfWriter       
      String dest = "C:/itextExamples/lineAnnotations.pdf";       
      PdfWriter writer = new PdfWriter(dest);               

      // Creating a PdfDocument       
      PdfDocument pdf = new PdfDocument(writer);               

      // Creating a Document        
      Document document = new Document(pdf);              
      
      // Creating a PdfPage       
      PdfPage page = pdf.addNewPage();              

      // creating PdfLineAnnotation object       
      Rectangle rect = new Rectangle(0, 0);
      float[] floatArray  = new float[]{
         20, 790, page.getPageSize().getWidth() - 20, 790
      };
      PdfAnnotation annotation = new PdfLineAnnotation(rect, floatArray);              
      
      // Setting color of the PdfLineAnnotation       
      annotation.setColor(Color.BLUE);              
      
      // Setting title to the PdfLineAnnotation       
      annotation.setTitle(new PdfString("iText"));              
      
      // Setting contents of the PdfLineAnnotation       
      annotation.setContents("Hi welcome to Howcodex");              
      
      // Adding annotation to the page       
      page.addAnnotation(annotation);               
      
      // 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 LineAnnotation.java 
java LineAnnotation 

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.

Line Annotation
Advertisements