The Wayback Machine - https://web.archive.org/web/20240616084807/https://www.geeksforgeeks.org/create-a-table-in-a-pdf-using-java/
Open In App

Create a Table in a PDF Using Java

Last Updated : 04 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The creation of a table in a PDF using Java is done by installing the document class. While instantiating this class, pass a PdfDocument object as a parameter to its constructor. Then, to feature a table to the document, instantiate the Table class, and add this object to the document using the add() method.

Note: External jar files are required to perform operations on PDF.

Below is an example PDF adding a table in a pdf using java with all the steps:

Java




// Adding table in a pdf using java
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
  
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
  
public class AddingTableToPDF {
    public static void main(String args[]) throws Exception
    {
        String file
            = "C:/EXAMPLES/itextExamples/addingTableToPDF.pdf";
  
        // Step-1 Creating a PdfDocument object
        PdfDocument pdfDoc
            = new PdfDocument(new PdfWriter(file));
  
        // Step-2 Creating a Document object
        Document doc = new Document(pdfDoc);
  
        // Step-3 Creating a table
        Table table = new Table(2);
  
        // Step-4 Adding cells to the table
        table.addCell(new Cell().add("Name"));
        table.addCell(new Cell().add("Raju"));
        table.addCell(new Cell().add("Id"));
        table.addCell(new Cell().add("1001"));
        table.addCell(new Cell().add("Designation"));
        table.addCell(new Cell().add("Programmer"));
  
        // Step-6 Adding Table to document
        doc.add(table);
  
        // Step-7 Closing the document
        doc.close();
        System.out.println("Table created successfully..");
    }
}


Output:



Similar Reads

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 this library for digital signature, print and val
7 min read
Setting Background to a Table in a PDF using Java
Have you ever wondered how different cells in a PDF table are set to various colors? Well, in this article we'll be seeing how we can set the background of various cells in a PDF table using the iText library in your favorite language Java. Downloading iText Create a new Java Maven project and add the following dependencies in the pom.xml file. Thi
6 min read
Formatting the Content of a Cell in a Table of PDF using Java
iText is a Java library developed, to access and manipulate PDF files, that is to extract and modify the PDF content. iText is a Java library originally created by Bruno Lowagie which allows creating, reading, and manipulating PDF files. Java allows incorporating various fully developed packages and modules in order to work with PDF files.iText lib
4 min read
How to Create Pivot Chart from Pivot Table in Excel using Java?
A Pivot Chart is used to analyze data of a table with very little effort (and no formulas) and it gives you the big picture of your raw data. It allows you to analyze data using various types of graphs and layouts. It is considered to be the best chart during a business presentation that involves huge data. To add a pivot chart to an Excel workshee
4 min read
How to Create Pivot Table in Excel using Java?
A pivot table is needed to quickly analyze data of a table with very little effort (and no formulas) and sometimes not everyone has time to look at the data in the table and see what’s going on and use it to build good-looking reports for large data sets in an Excel worksheet. Let's discuss a step-by-step proper explanation to create a pivot table
5 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 this class accepts a string, representing the tra
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 the iText library. 1. PdfWriter Java has an inbuilt
4 min read
Setting the Position of the Image in PDF Document using Java
To set the Position of the Image in a PDF document using Java multiple external dependencies need to download first. Setting the Position of the Image in a PDF, use the iText library. These are the steps that should be followed to Set the Position of the Image in a PDF using java. 1. Creating a PdfWriter object: The PdfWriter class represents the D
3 min read
Encrypt PDF using Java
We can encrypt any PDF using Java by using the external library PDFBox. Inside PDFBox library 2 classes are available StandardProtectionPolicy and AccessPermission Class. Encryption Approach: By using the PDFBox library, you will see how you can encrypt the PDF file. Encryption is used when a user wants their own data or file in protected mode. Enc
4 min read
Decrypt PDF using Java
We can Decrypt any PDF using Java by using the external library PDFBox. We can Decrypt and remove the Access Permission from any PDF using JAVA, but we must remember the Owner Password of the PDF to Decrypt it, otherwise, we can not decrypt it. Approach: In this program, you will see how you can take an encrypted PDF file as input and how you can d
3 min read
Article Tags :
Practice Tags :