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 :
Example : Drawing a circle in a PDF
- Get the current working directory of the running java program to create the PDF file in the same location
- Create a PdfWriter object (from itextpdf library) which writes the PDF file to the given path
- Create an empty PdfDocument object and add a page to it using PdfPage object
- Create the canvas using the PdfCanvas object in the Pdf Page
- Create the object on the Canvas and fill the object with color
Below is the implementation of the above approach:
The PDF file is created in the specified path.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.