iText - Drawing a Circle

In this chapter, we will see how to draw a circle on a PDF document using iText library.
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 circle on a PdfDocument, instantiate the PdfCanvas class of the package com.itextpdf.kernel.pdf.canvas and invoke the circle() method of this class.
Following are the steps to draw a circle on a PDF document.
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/drawingCircle.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.
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 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.
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); 

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 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); 

Set the color of the circle using the setColor() method of the Canvas class, as shown below.
// Setting color to the circle 
Color color = Color.GREEN; 
canvas.setColor(color, true); 

Draw a circle by invoking the circle() method of the Canvas, as shown below.
// creating a circle 
canvas.circle(300, 400, 200); 

Close the document using the close() method of the Document class, as shown below.
// Closing the document 
document.close(); 

The following Java program demonstrates how to draw a circle on a pdf document using the iText library. It creates a PDF document with the name drawingCircle.pdf, draws a circle in it, and saves it in the path C:/itextExamples/
Save this code in a file with the name DrawingCircle.java.
import com.itextpdf.kernel.color.Color; 
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 DrawingCircle {      
   public static void main(String args[]) throws Exception {           
      // Creating a PdfWriter       
      String dest = "C:/itextExamples/drawingCircle.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);  
      
      // Setting color to the circle
      Color color = Color.GREEN;       
      canvas.setColor(color, true);              
      
      // creating a circle
      canvas.circle(300, 400, 200);
      
      // Filling the circle       
      canvas.fill();             
      
      // 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 DrawingCircle.java 
java DrawingCircle

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.
 Print
 Add Notes
 Bookmark this page