Drawing a Line in a PDF Document using Java
Last Updated :
17 Jun, 2021
In this article, we will learn how to Draw a line in a PDF document using Java. For drawing a line in a PDF, we will use the iText library. These are the steps that should be followed to Draw a line in a PDF using java.
1. Creating a PdfWriter object
The PdfWriter class represents the DocWriter for a PDF. The constructor of this class accepts a string, i.e. the path of the file where the PDF is to be created.
// importing the PdfWriter class.
import com.itextpdf.kernel.pdf.PdfWriter;
// path where the pdf is to be created.
String path = "F:/JavaPdf/DrawLine.pdf";
PdfWriter pdfwriter = new PdfWriter(path);
2. Creating a PdfDocument object
The PdfDocument class is that the class that represents the PDF Document in iText, To instantiate this class in write mode, you would like to pass an object of the category PdfWriter (i.e. pdfwriter from above code) to its constructor.
// Creating a PdfDocument object.
// passing PdfWriter object constructor of pdfDocument.
PdfDocument pdfdocument = new PdfDocument(pdfwriter);
3. Creating the Document object
The Document class is the root element when creating a self-sufficient PDF. One of the constructors of this class accepts an object of the type PdfDocument class(i.e. pdfdocument).
// Creating a Document and passing pdfDocument object
Document document = new Document(pdfdocument);
4. Creating a PdfCanvas object
Before Instantiate the PdfCanvas object, We have to create a new pdfPage object as we need to pass pdfPage object to the constructor of PdfCanvas class.
// Creating a new page
PdfPage pdfPage = pdfdocument.addNewPage();
// instantiating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
5.Drawing the line and closing the path stroke, document.
Set the starting point of the line using the moveTO() method of the Canvas class and  draw till the endpoint using the lineTo() method of the Canvas class.
// starting point of the line
canvas.moveTo(100, 300);
// Drawing the line till the end point.
canvas.lineTo(500, 300);
// Close the path stroke
canvas.closePathStroke();
// Close the document
document.close();
Dependencies for executing the below program:
kernel-7.1.13.jar
layout-7.1.13.jar
Example: Code to draw a line in a PDF document using java.
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 DrawLine {
public static void main(String args[]) throws Exception
{
try {
// path where the pdf is to be created.
String path = "F:/JavaPdf/DrawLine.pdf";
PdfWriter pdfwriter = new PdfWriter(path);
// Creating a PdfDocument object.
// passing PdfWriter object constructor of
// pdfDocument.
PdfDocument pdfdocument
= new PdfDocument(pdfwriter);
// Creating a Document and passing pdfDocument
// object
Document document = new Document(pdfdocument);
// Creating a new page
PdfPage pdfPage = pdfdocument.addNewPage();
// instantiating a PdfCanvas object
PdfCanvas canvas = new PdfCanvas(pdfPage);
// starting point of the line
canvas.moveTo(100, 500);
// Drawing the line till the end point.
canvas.lineTo(500, 500);
// close the path stroke
canvas.closePathStroke();
// Close the document
document.close();
System.out.println(
"drawn the line successfully");
}
catch (Exception e) {
System.out.println(
"Failed to draw the line due to " + e);
}
}
}
 Â
Compile and execute the program using the following commands
Â
javac RotateImage.java
java RotateImage
Â
Output
Â
drawn the line successfully
Â
PDF
Â
Â
Similar Reads
Adding List in a PDF using Java In this article, we will learn how to create a PDF and add a list to that PDF using java. For adding a list in a PDF, we will use the iText library. These are the steps that should be followed to add a list in a PDF using java. 1. Creating a PdfWriter object The PdfWriter class represents the DocWri
3 min read
Drawing an Object in a PDF with Canvas using Java To create an object in a PDF with Canvas using Java can be done by using a library called iText. iText is a Java library originally created by Bruno Lowagie which allows creating PDF, read PDF, and manipulate them. Libraries required : iText slf4j (Logging Library)Example : Drawing a circle in a PDF
2 min read
Rotating an Image in a PDF Document Using Java In this article, we will learn how to  Rotating an Image in a PDF document using Java. For  Rotating an Image in a PDF, we will use the iText library. These are the steps that should be followed to  Rotating an Image in a PDF using java. 1. Creating a PdfWriter object The PdfWriter class represents
3 min read
Java Applet | Draw a line using drawLine() method This article shall be explaining the code to draw a line using paint in Java. This uses drawLine() method. Syntax: drawLine(int x1, int y1, int x2, int y2) Parameters: The drawLine method takes four arguments: x1 - It takes the first point's x coordinate. y1 - It takes first point's y coordinate. x2
2 min read
Adding Pages to a PDF Document using Java PDDocument class of 'org.apache.pdfbox.pdmodel' package which extends 'java.lang.Object'. is used. Declaration: public class PDDocument extends Object implements Pageable, Closeable Pre-requisite: Constructors PDDocument(): This constructor used to construct a new PDF document with zero pages.PDDocu
4 min read
Removing Pages from a PDF Document using Java Program to remove pages from an existing document a PDF document. The external jar file is required to import in the program. Below is the implementation for the same. Remove a page from an existing PDF document using the removePage() method of the PDDocument class. Approach: Load an existing PDF do
2 min read