iText Introduction
- iText - Adding a List
- iText - Adding a Paragraph
- iText - Adding an AreaBreak
- iText - Creating a PDF Document
- iText - Overview
iText Tables
- iText - Adding Lists to a Table
- iText - Nested Table
- iText - Adding Image to a Table
- Formatting the Borders of a Cell
- iText - Formatting Cell Contents
- iText - Adding a Table
iText Images
- iText - Rotating an Image
- iText - Scaling an Image
- iText - Setting Position of the Image
- iText - Adding Image to a PDF
iText Annotations
- iText - Circle Annotation
- iText - Markup Annotation
- iText - Line Annotation
- iText - Link Annotation
- iText - Text Annotation
iText Canvas
iText Miscellaneous
iText Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
iText - Drawing a Line
In this chapter, we will see how to draw a pne on a PDF document using iText pbrary.
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 pne on a PdfDocument Instantiate the PdfCanvas class of the package com.itextpdf.kernel.pdf.canvas and create a pne using the moveTo() and pneTO() methods of this class.
Following are the steps to draw a pne 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 pke 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 pne
Set the initial point of the pne using the moveTO() method of the Canvas class, as shown below.
// Initial point of the pne canvas.moveTo(100, 300);
Now, draw a pne from this point to another point using the pneTo() method, as shown below.
// Drawing the pne canvas.pneTo(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 pne in a PDF document using the iText pbrary. 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; pubpc class DrawingLine { pubpc 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 pne canvas.moveTo(100, 300); // Drawing the pne canvas.pneTo(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](/itext/images/drawing_pne.jpg)