USING IRONPDF FOR JAVA

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

This article will demonstrate how to create tables inside a PDF document using Java.

It is necessary to have 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 performs effectively.

IronPDF is one library that's very useful for manipulating PDF files. You can find more information about IronPDF by clicking on the IronPDF Official Java Page.

The following steps are covered 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 the PDF document

Create a New Java Project

Open your preferred IDE. IntelliJ is recommended 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 Create a Table in a PDF Document Using Java (Tutorial), 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, install the IronPDF library in the newly created project. Continue with the following steps.

Open the pom.xml file and add the necessary dependencies and repositories to use IronPDF. The specific content for the pom.xml is not provided in this article, but make sure to properly include the IronPDF library using Maven's dependency management.

<!-- Add IronPDF dependencies here -->
<dependencies>
    <!-- Example dependency for IronPDF, actual group and artifact id to be retrieved from official documentation -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>VERSION_NUMBER</version>
    </dependency>
</dependencies>
<!-- Add IronPDF dependencies here -->
<dependencies>
    <!-- Example dependency for IronPDF, actual group and artifact id to be retrieved from official documentation -->
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>VERSION_NUMBER</version>
    </dependency>
</dependencies>
XML

Type the following command into your terminal and press enter to install the specified Maven dependencies.

mvn install
mvn install
SHELL

This will install IronPDF in this project.

Add the following import statement for using IronPDF classes.

import com.ironsoftware.ironpdf.*;
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 {
    // Create a PDF document from an HTML string
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("This is a sample PDF file");

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
public static void main(String[] args) throws IOException {
    // Create a PDF document from an HTML string
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("This is a sample PDF file");

    try {
        // Save the created PDF document to a file
        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, shown in the following image.

How to Create a Table in a PDF Document Using Java (Tutorial), Figure 2: New PDF Document New PDF Document

Create table for the PDF file

The following code will create a table in PDF.

public static void main(String[] args) throws IOException {
    // HTML content for creating a table
    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>";

    // Create a PDF document with table content
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Example of adding table in a PDF</h1>" + tableContent);

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
public static void main(String[] args) throws IOException {
    // HTML content for creating a table
    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>";

    // Create a PDF document with table content
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Example of adding table in a PDF</h1>" + tableContent);

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
JAVA

The above code uses 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 this program is shown in the following image:

How to Create a Table in a PDF Document Using Java (Tutorial), Figure 3: PDF document containing a table from HTML PDF document containing a table from HTML

It's a simple table with no styling.

Now let's add some styling to this table, such as set table width, margin, layout, font, background color, and 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 {
    // HTML and CSS content for styling the table
    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>";

    // Create a PDF document with styled table content
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(htmlStyle + "Sample PDF" + tableContent);

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
public static void main(String[] args) throws IOException {
    // HTML and CSS content for styling the table
    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>";

    // Create a PDF document with styled table content
    PdfDocument myPdf = PdfDocument.renderHtmlAsPdf(htmlStyle + "Sample PDF" + tableContent);

    try {
        // Save the created PDF document to a file
        myPdf.saveAs(Paths.get("html_saved.pdf"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
JAVA

The added CSS is used for styling the table in the PDF. Hence, it is very efficient to use CSS for styling as needed.

Following is the PDF generated by this program.

How to Create a Table in a PDF Document Using Java (Tutorial), Figure 4: PDF document containing a table from HTML and styled with CSS PDF document containing a table from HTML and styled with CSS

This above code looks very messy. But it can be cleaned by moving all the HTML content into an HTML file and then generating the PDF from that file.

Create a table using an HTML file in PDF using Java

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

How to Create a Table in a PDF Document Using Java (Tutorial), Figure 5: HTML moved into its own HTML file HTML moved into its own HTML file

Add the following code to the Java program.

// Create a PDF document from an HTML file
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("Create_Table.html");

try {
    // Save the created PDF document to a file
    myPdf.saveAs(Paths.get("html_saved.pdf"));
} catch (IOException e) {
    throw new RuntimeException(e);
}
// Create a PDF document from an HTML file
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("Create_Table.html");

try {
    // Save the created PDF document to a file
    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 a single line of code.

How to Create a Table in a PDF Document Using Java (Tutorial), Figure 6: Final PDF document with styling Final PDF document with styling

Summary

This tutorial demonstrated how to create a styled table in a PDF file using Java and learned to convert HTML files into PDF files. IronPDF for Java also offers functionality to add images to the PDF file, split PDF files, add headers and footers, apply digital signatures, and much more. Explore IronPDF Documentation to learn more about these features and additional capabilities.

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

Frequently Asked Questions

What is required to create tables in PDF documents using Java?

A third-party library is required to create tables in PDF documents using Java. IronPDF is one such library that is easy to use and effective.

How can I install the IronPDF library in my Java project?

To install IronPDF, you must add its dependencies to your project's pom.xml file and install them using Maven's 'mvn install' command.

How do I create a simple PDF document using IronPDF in Java?

You can create a simple PDF document by using the PdfDocument.renderHtmlAsPdf method to convert an HTML string into a PDF document.

How can I add a table to a PDF document using Java?

You can add a table by using HTML table tags within a string and then converting that string into a PDF using IronPDF's rendering methods.

Can I style tables in PDF documents created with IronPDF?

Yes, you can style tables using CSS within the HTML string before rendering it into a PDF. This allows you to control the table's appearance, including fonts, borders, and colors.

Is it possible to use an HTML file to create a PDF with IronPDF?

Yes, you can create a PDF from an HTML file by using the PdfDocument.renderHtmlFileAsPdf method, which converts the entire HTML file into a PDF document.

What additional features does IronPDF offer for PDF manipulation?

IronPDF provides features like adding images, splitting PDFs, adding headers and footers, and applying digital signatures, among others.

How can I remove the IronPDF watermark from my PDF documents?

You can remove the IronPDF watermark by purchasing a license key or registering for a free trial license.

Darrius Serrant
Full Stack Software Engineer (WebOps)

Darrius Serrant holds a Bachelor’s degree in Computer Science from the University of Miami and works as a Full Stack WebOps Marketing Engineer at Iron Software. Drawn to coding from a young age, he saw computing as both mysterious and accessible, making it the perfect medium for creativity and problem-solving.

At Iron Software, Darrius enjoys creating new things and simplifying complex concepts to make them more understandable. As one of our resident developers, he has also volunteered to teach students, sharing his expertise with the next generation.

For Darrius, his work is fulfilling because it is valued and has a real impact.

< PREVIOUS
How to Convert PNG to PDF in Java (Tutorial)
NEXT >
PDF Creator For Java (Step-By-Step) Tutorial

Install with Maven

Version: 2025.6.5

<dependency>
  <groupId>com.ironsoftware</groupId>
  <artifactId>ironpdf</artifactId>
  <version>2025.6.5</version>
</dependency>