iText – Drawing an Arc

itext drawing an arc

In this chapter, we will discuss about itext drawing an arc and see how to draw an arc on a PDF document using iText library.

Drawing an Arc 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 an arc on a PdfDocument, instantiate the PdfCanvas class of the package com.itextpdf.kernel.pdf.canvas and create an arc using the arc() method of this class.

Following are the steps to draw an arc on 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/drawingArc.pdf"; 
PdfWriter writer = new PdfWriter(dest); 

When the 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 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 arc

Draw the arc using the arc() method of the Canvas class and fill it using the fill() method, as shown below.

// Drawing an arc 
canvas.arc(50, 50, 300, 545, 0, 360);   

// Filling the arc 
canvas.fill(); 

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 an arc in a PDF document using the iText library.

It creates a PDF document with the name drawingArc.pdf, draws an arc in it, and saves it in the path C:/itextExamples/

Save this code in a file with the name DrawingArc.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 DrawingArc {
   public static void main(String args[]) throws Exception {
      // Creating a PdfWriter
      String dest = "C:/itextExamples/drawingArc.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);

      // Drawing an arc
      canvas.arc(50, 50, 300, 545, 0, 360);

      // Filling the arc
      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 DrawingArc.java 
java DrawingArc

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 an arc

Next Topic : Click Here

This Post Has One Comment

Leave a Reply