Adding List in a PDF using Java - GeeksforGeeks

04 Oct, 2021
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 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.
Java
// importing the PdfWriter class.import com.itextpdf.kernel.pdf.PdfWriter; // path where the pdf is to be created.String path = "C:/JavaPdf/addingList.pdf";PdfWriter pdfwriter = new PdfWriter(path);
2. Creating a PdfDocument object
The PdfDocument class is the class that represents the PDF Document in iText, To instantiate this class in write mode, you need to pass an object of the class PdfWriter (i.e. pdfwriter from above code ) to its constructor.
Java
// 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 class PdfDocument (i.e. pdfdocument).
Java
// Creating a Document and passing pdfDocument object Document document = new Document(pdfdocument);
4. Creating a List object and adding elements to the list object.
The List represents a series of objects that are vertically oriented. We add elements to the list object using the add() method of the List class.
Java
// Creating a list List list = new List(); //  Adding contents to the listlist.add("geekforgeeks");list.add("helps");list.add("to");list.add("master");list.add("DSA");
5. Here comes the final part Adding List objects to the document.
Add the list object using the add() method of the Document class, and close the document using close() method of Document class 
Java
// Adding list to the documentdocument.add(list); // Closing the documentdocument.close();
Example: Here is the final code that helps us in understanding how to add a list in PDF using java. 
Java
// Java program to add a list in a PDF import com.itextpdf.kernel.pdf.PdfDocument;import com.itextpdf.kernel.pdf.PdfWriter;import com.itextpdf.layout.element.List;import com.itextpdf.layout.element.Paragraph;import com.itextpdf.layout.Document; public class AddList {    public static void main(String args[])    {        try {            // path where the pdf is to be created.            String path = "C:/JavaPdf/addingList.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 list            List<String> list = new ArrayList();             // Adding contents to the list            list.add("geekforgeeks");            list.add("helps");            list.add("to");            list.add("master");            list.add("DSA");             // Adding list to the document            document.add(list);             // Closing the document            document.close();            System.out.println(                "List has been successfully added to the file :"                + path);        }        catch (Exception e) {            System.out.println(                "failed to add the list to file due to "                + e);        }    }}
Compile and execute the saved Java file from the Command prompt using the following commands: 
javac AddList.java 
java AddList
Output 
pdf with a list added using java
 
anikaseth98
surindertarika1234
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Stream In Java
Constructors in Java
Generics in Java
Exceptions in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?