We will discuss how a PDF doc is created and how to draw a circle in it using iText library.
By instantiating the Document class an empty PDF Document can be created. a PdfDocument object should be passed as a parameter while instantiating this class to its constructor.
To create a line using the circle() method, firstly the PdfCanvas class of the package com.itextpdf.kernel.pdf.canvas should be instantiated and add it to PdfPage.
Below are the steps to follow for drawing a circle in the PDF document.
Step 1: Creating a PdfWriter object
The package which this class belongs to is com.itextpdf.kernel.pdf. It accepts a string, that takes the path where the PDF should be created.
As shown below, lets start with Instantiating the PdfWriter class by passing a string value to its constructor.
After creating this object and passing to a PdfDocument (class) will add every element added to this document to the file specified in the path.
Step 2: Creating a PdfDocument object
The PdfDocument class is the class that represents the PDF Document in iText. The package com.itextpdf.kernel.pdf is where this class belongs to. We have to pass an object of class PdfWriter to its constructor to instantiate this class in writing mode.
Below is what a PdfDocument class Instantiation done by passing the above created PdfWriter object to its constructor.
We have methods for each element like page, file attachment, font, event handler which are provided by its class once a PdfDocument object is created.
Step 3: Creating the Document object
For creating a self-sufficient PDF the Document class of the package com.itextpdf.layout is the root element. Object of the class PdfDocument is one of the constructors of this class.
Follow the step below to instantiate the Document class by passing the object of the class PdfDocument created in earlier steps.
Step 4: Creating a PdfCanvas object
The PdfCanvas class of the package com.itextpdf.kernel.pdf.canvas should be instantiated and pass the PdfPage object to the constructor. New PdfPage class is created using the addNewPage() method of the PdfDocument class.
Step 5 Setting the color
Using the setColor() method of the Canvas class, set the color of the circle.
Step 6: Drawing the Circle
A circle can be drawn by invoking the circle() method of the Canvas, as below.
Step 7: Closing the Document
Use close() method of the Document class to close the document below.
Below Java program demonstrates the PDF Document creation and drawing a circle using the iText library. A PDF doc with name drawingCircle.pdf is created, a circle is drawn to it, and saves it in the path C:/itextExamples/.
Paste the below code in file and give its name as DrawingCircle.java.
Using Command prompt, Compile and execute the saved Java file using the following commands –
We will get the below message once above code is executed.
The path which we gave while creating a PDF will have the PDF created as below.
iText Related Interview Questions
|C++ Interview Questions||Adv Java Interview Questions|
|Core Java Interview Questions||Java Interview Questions|
|Advanced C++ Interview Questions||C and C++ Interview Questions|
|Ericsson JAVA Interview Questions||Executors Java Interview Questions|
|Java 9 Interview Questions|
All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.