iText - Drawing a Line



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

Drawing a Line on 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 draw a line on a PdfDocument Instantiate the PdfCanvas class of the package com.itextpdf.kernel.pdf.canvas and create a line using the moveTo() and lineTO() methods of this class.

Following are the steps to draw a line on 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 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/drawingLine.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 above created 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, 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 a PdfCanvas object

Create a new PdfPage class using the addNewPage() method of the PdfDocument class.

Instantiate the PdfCanvas object of the package com.itextpdf.kernel.pdf.canvas by passing the above created PdfPage object to the constructor of this class, as shown below.

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

// Creating a PdfCanvas object 
PdfCanvas canvas = new PdfCanvas(pdfPage); 

Step 5: Drawing the line

Set the initial point of the line using the moveTO() method of the Canvas class, as shown below.

// Initial point of the line 
canvas.moveTo(100, 300); 

Now, draw a line from this point to another point using the lineTo() method, as shown below.

// Drawing the line 
canvas.lineTo(500, 300); 

Step 6: 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 draw a line in a PDF document using the iText library. It creates a PDF document with the name drawingLine.pdf, draws an arc in it, and saves it in the path C:/itextExamples/

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

import com.itextpdf.kernel.pdf.PdfDocument; 
import com.itextpdf.kernel.pdf.PdfPage; 
import com.itextpdf.kernel.pdf.PdfWriter; 
import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 
import com.itextpdf.layout.Document;  

public class DrawingLine {     
   public static void main(String args[]) throws Exception {            
      // Creating a PdfWriter       
      String dest = "C:/itextExamples/drawingLine.pdf";             
      PdfWriter writer = new PdfWriter(dest);           
      
      // Creating a PdfDocument object       
      PdfDocument pdfDoc = new PdfDocument(writer);                   
      
      // Creating a Document object       
      Document doc = new Document(pdfDoc);                
      
      // Creating a new page       
      PdfPage pdfPage = pdfDoc.addNewPage();               
      
      // Creating a PdfCanvas object       
      PdfCanvas canvas = new PdfCanvas(pdfPage);              
      
      // Initial point of the line       
      canvas.moveTo(100, 300);              
      
      // Drawing the line       
      canvas.lineTo(500, 300);           
      
      // Closing the path stroke       
      canvas.closePathStroke();              
      
      // Closing the document       
      doc.close();  
      
      System.out.println("Object drawn on pdf successfully");             
   }     
}

Compile and execute the saved Java file from the Command prompt using the following commands −

javac DrawingLine.java 
java DrawingLine

Upon execution, the above program creates a PDF document, displaying the following message.

Object drawn on pdf successfully 

If you verify the specified path, you can find the created PDF document, as shown below.

Drawing Line
Advertisements