iText is a Java library developed, to access and manipulate PDF files, that is to extract and modify the PDF content. Java allows us to incorporate various fully developed packages and modules in order to work with PDF files. We will see how to create a PDF document and add a paragraph to it using the iText library.
Java has an inbuilt package com.itextpdf.kernel.pdf, which basically provides classes and modules to create PDF documents in Java. An available class in this package is PdfWriter. We instantiate an object of this class and pass as argument the file-path along with the name of the new PDF file we wish to create. An object is passed onto this class, in order to append text to the specified file location. The following Java code snippet illustrates the usage of this class :
// path to create the file String file_path = "C:/appendtexttopdfjava.pdf"; // creating an object of PdfWriter class with file_path as argument PdfWriter pdf_writer = new PdfWriter(file_path);
This creates a new PDF file in the C: with the name as appendtexttopdfjava.pdf.
The package com.itextpdf.kernel.pdf contains another class to represent the specified PDF file in iText which provides a user with the ease of adding various features like page fonts, file attachment by incorporating various methods of this class. An object needs to be instantiated of this class by passing as argument the pdf_writer object created using PdfWriter class. The following Java code snippet illustrates the usage of this class :
// Representing PDF document in iText PdfDocument pdf_doc = new PdfDocument(pdf_writer);
The Document class of the package com.itextpdf.layout takes as an argument the PdfDocument object created and instantiates the Document used as a source for all PDF operations to be performed. It acts as a container for content that needs to be modified or appended to the document file.
// Instantiating a document object from pdf document object Document document = new Document(pdf_doc);
When an object of this class is created, the file is visualized as a stream of characters, to which operations (addition of new characters, modification of previous ones, deletion, etc. ) can be performed upon.
The Paragraph class of the Java inbuilt package com.itextpdf.layout.element is basically a child of the Document class. It creates an object using the text stream, that is the contents that are actually going to be added to the PDF document. The content may be a block of lines, which needs to be added to the document class object using the add() method provided by the document class. The paragraph class is basically an element of the document class. Multiple paragraph objects can be created and added to the same document.
After writing the content to the document, it is closed.
//content to be added to the pdf document String paragraph = "Geeks For Geeks makes you learn coding. It also provides competitions"; //Creating a paragraph class object Paragraph para_obj = new Paragraph (paragraph); //adding paragraph to the document object document.add(para_obj); //closing the document after writing the contents document.close();
The following Java code indicates the function of addition of a paragraph to the Java file :
The code upon execution on the terminal produces the following output on the terminal and saves a file in C: at the local computer.
Finished writing contents to the file!
The contents of the saved PDF file are as follows :
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.