Related Articles

Related Articles

Adding List in a PDF using Java
  • Last Updated : 17 Nov, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating a PdfDocument  object.
// passing PdfWriter object constructor of pdfDocument.
PdfDocument pdfdocument = new PdfDocument(pdfwriter); 

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating a Document and passing pdfDocument object  
Document document = new Document(pdfdocument);

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Creating a list  
List list = new List();
  
//  Adding contents to the list 
list.add("geekforgeeks"); 
list.add("helps"); 
list.add("to"); 
list.add("master");
list.add("DSA");

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Adding list to the document 
document.add(list);
  
// Closing the document 
document.close();

chevron_right


Example: Here is the final code that helps us in understanding how to add a list in PDF using java.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to addd 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 sucessfully added to the file :"
                + path);
        }
        catch (Exception e) {
            System.out.println(
                "failed to add the list to file due to "
                + e);
        }
    }
}

chevron_right


Compile and execute the saved Java file from the Command prompt using the following commands:

javac AddList.java 
java AddList

Output

Adding list to a pdf

pdf with a list added using java


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :