Adding List in a PDF using Java
Last Updated :
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 list
list.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 document
document.add(list);
// Closing the document
document.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
Similar Reads
Drawing a Line in a PDF Document using Java 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
3 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
Adding Nested Tables to a PDF using Java We can add nested tables to a PDF by installing the document class. While instantiating this class, you would like to pass a PdfDocument object as a parameter, to its constructor. Then, to feature a table to the document, you would like to instantiate the Table class and add this object to the docum
4 min read
Adding Images to a Table in PDF using Java PDFBox is an open-source library which is written in Java. It helps in the development and conversion of PDF Documents. PDFBox library comes in form of a JAR file. It can create new PDF documents, manipulate existing documents, bookmark the PDF and also extract content from PDF documents. We can use
7 min read
Formatting the Text in a PDF using Java We can add nested tables to a PDF by installing the document class. Following are the steps to format the text in a PDF using java. 1. Create a PDF writer object The PdfWriter class here represents the DocWriter for a PDF. This class belongs to the package com.itextpdf.kernel.pdf. The constructor of
3 min read
Adding Paragraphs as Text to a PDF using Java 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 t
4 min read