How to Create a Table in a PDF Document Using Java (Tutorial)

In this article, we'll learn how to create tables inside a PDF document using Java.

We'll need a third-party library for creating tables in PDF documents using a Java program. There are multiple libraries available that can be used to create tables inside PDFs using a Java program. However, they can be expensive, challenging to use, or have performance issues. It can be tricky to find a library that is easy to use, free for development, and that is performant.

IronPDF is one library that's very useful for manipulating PDF files. You can find more information about IronPDF by clicking here.

We will cover the following steps in this article:

  1. Create a new project
  2. Install IronPDF Library
  3. Create a new PDF document
  4. Create a table for it
  5. Add dynamic values to our PDF document

Create a New Java Project

Open your preferred IDE. We will be using IntelliJ in this article, so the steps to create a new project may differ using another IDE.

Open IntelliJ IDE, click on File-> New Project from the top menu bar. Name your project, select the location, language, build system, and JDK as shown below.

How to Add a Table to a PDF Using Java - Figure 1: IntelliJ IDE New Project Window

IntelliJ IDE New Project Window

Click on the Create Project button, and a new project will be created.

Install IronPDF Library

Now, we need to install the IronPDF library in our newly created project. Continue with the following steps.

Open the pom.XML file and add the following XML content under the properties closing tag.

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2024.3.1</version>
</dependency>
XML

Type the following command into your terminal and press enter.

mvn install

This will install IronPDf to our project.

Add the following import statement for using IronPDF classes.

import com.ironsoftware.ironpdf.*;
JAVA

First of all, we will learn to create simple PDF documents in java.

Create a PDF document

The following example code will create a new PDF document.

public static void main(String[] args) throws IOException {
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("This is sample pdf file");
    try {
        myPdf.saveAs(Paths.get("html_saved.pdf") );
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
JAVA

The renderHtmlAsPDF method takes a string as an argument and converts that string into an instance of a PDF document.

The saveAs function takes a file path as an argument and saves the newly created PDF document to the file path specified in the argument.

A PDF is created from the above code, in the following image.

How to Add a Table to a PDF Using Java - Figure 2: New PDF Document

New PDF Document

Now, we'll add a table to the PDF.

Create table for the PDF file

The following code will create a table in PDF.

public static void main(String[] args) throws IOException {

    String tableContent = "<table>\n" +
            "  <tr>\n" +
            "    <th>Company</th>\n" +
            "    <th>Contact</th>\n" +
            "    <th>Country</th>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Alfreds Futterkiste</td>\n" +
            "    <td>Maria Anders</td>\n" +
            "    <td>Germany</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Centro comercial Moctezuma</td>\n" +
            "    <td>Francisco Chang</td>\n" +
            "    <td>Mexico</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Ernst Handel</td>\n" +
            "    <td>Roland Mendel</td>\n" +
            "    <td>Austria</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Island Trading</td>\n" +
            "    <td>Helen Bennett</td>\n" +
            "    <td>UK</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Laughing Bacchus Winecellars</td>\n" +
            "    <td>Yoshi Tannamuri</td>\n" +
            "    <td>Canada</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Magazzini Alimentari Riuniti</td>\n" +
            "    <td>Giovanni Rovelli</td>\n" +
            "    <td>Italy</td>\n" +
            "  </tr>\n" +
            "</table>\n";
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Example of adding table in a PDF</h1>" + tableContent);
    try {
        myPdf.saveAs(Paths.get("html_saved.pdf") );
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
JAVA

We can see that we have used just simple HTML tags to create a table in PDF using Java. So, to create a table, you must have basic knowledge of using HTML tags. Almost every Java programmer has knowledge of HTML, so it is very easy to create a new table and table cells using HTML tags.

The PDF file generated by our program is in the following image:

How to Add a Table to a PDF Using Java - Figure 3: PDF document containing a table from HTML

A PDF Document containing a table created from HTML

We can see that it's a simple table with no styling.

Now we'll add some styling to our table such as set table width, margin, layout, font, background color, and many more.

Add Styling to the Table

The following sample code will format our table, and add styling to our cells.

public static void main(String[] args) throws IOException {
    String htmlStyle = "<!DOCTYPE html>\n" +
        "<html>\n" +
            "<head>\n" +
                "<style>\n" +
                    "table {\n" +
                    "  font-family: arial, sans-serif;\n" +
                    "  border-collapse: collapse;\n" +
                    "  width: 100%;\n" +
                    "}\n" +
                    "\n" +
                    "td, th {\n" +
                    "  border: 1px solid #dddddd;\n" +
                    "  text-align: left;\n" +
                    "  padding: 8px;\n" +
                    "}\n" +
                    "\n" +
                    "tr:nth-child(even) {\n" +
                    "  background-color: #dddddd;\n" +
                    "}\n" +
                "</style>\n" +
            "</head>\n" +
        "<body>";
    String tableContent = "<table>\n" +
            "  <tr>\n" +
            "    <th>Company</th>\n" +
            "    <th>Contact</th>\n" +
            "    <th>Country</th>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Alfreds Futterkiste</td>\n" +
            "    <td>Maria Anders</td>\n" +
            "    <td>Germany</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Centro comercial Moctezuma</td>\n" +
            "    <td>Francisco Chang</td>\n" +
            "    <td>Mexico</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Ernst Handel</td>\n" +
            "    <td>Roland Mendel</td>\n" +
            "    <td>Austria</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Island Trading</td>\n" +
            "    <td>Helen Bennett</td>\n" +
            "    <td>UK</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Laughing Bacchus Winecellars</td>\n" +
            "    <td>Yoshi Tannamuri</td>\n" +
            "    <td>Canada</td>\n" +
            "  </tr>\n" +
            "  <tr>\n" +
            "    <td>Magazzini Alimentari Riuniti</td>\n" +
            "    <td>Giovanni Rovelli</td>\n" +
            "    <td>Italy</td>\n" +
            "  </tr>\n" +
            "</table>\n</body>\n" +
            "</html>";
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(htmlStyle+"Sample PDF"+tableContent);
    try {
        myPdf.saveAs(Paths.get("html_saved.pdf") );
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
JAVA

We have just added CSS for styling our table in the PDF. Hence; we can style our table as we want using CSS in a very easy way.

Following is the PDF generated by our program.

How to Add a Table to a PDF Using Java - Figure 4: PDF document containig a table form HTMl and styled with CSS

A PDF Document containing a table created from HTML that is styled using CSS

Our code looks very messy. We can clean it up by moving all our HTML content into an HTML file and then generating our PDF using that file.

Create a table using an HTML file in PDF using Java

Create a new HTML file, and add your all HTML content to that file as shown below:

How to Add a Table to a PDF Using Java - Figure 5: HTML moved into its own HTML file

HTML moved from the Java program into its own file

Add the following code to the Java program.

PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("Create_Table.html");
try {
    myPdf.saveAs(Paths.get("html_saved.pdf") );
} catch (IOException e) {
    throw new RuntimeException(e);
}
JAVA

We can see how easy it is to generate a PDF using an HTML document. If you need to create PDF files in java, the easiest way is to create a separate HTML document with content and styling, and then just convert your HTML document into PDF with just a single line of code.

Final PDF document with styling

How to Add a Table to a PDF Using Java - Figure 6: Final PDF document with styling

The final PDF document with the styled table

Summary

In this tutorial, we have learned to create a styled table in a PDF file using Java. We have also learned to convert HTML files into PDF files using Java. We can also add images to our PDF, split the cells, add new columns, rows, paragraphs, headers, footers, digital signatures, and many more. Learn more about these features and much more from IronPDF Documentation pages.

Remove the IronPDF watermark from your generated PDF documents by purchasing a license key or by registering for a free trial.

I have tried to keep this tutorial as simple as possible, if you have any questions, feel free to ask in the comments section.