USING IRONPDF FOR JAVA

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

Updated August 25, 2024
Share:

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 perform 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 following XML content under the properties closing tag.

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

Type the following command into your terminal and press enter.

mvn install

This will install IronPDF in this 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 a 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, 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 {

    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

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 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

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.

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 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.

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

Install with Maven

Version: 2024.11.4

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

Ready to get started? Version: 2024.11 just released

Free Maven Download View Licenses >