Java: Create Tables in a PDF Document

A table represents information or data in the form of horizontal rows and vertical columns. Creating tables is often more efficient than describing the data in the paragraph text, especially when the data is numerical or large. The tabular data presentation makes it easier to read and understand. In this article, you will learn how to create tables in a PDF document in Java using Spire.PDF for Java.

Spire.PDF for Java offers the PdfTable and the PdfGrid class to work with the tables in a PDF document. The PdfTable class is used to quickly create simple, regular tables without too much formatting, while the PdfGrid class is used to create more complex tables.

The table below lists the differences between these two classes.

PdfTable PdfGrid
Formatting
Row Can be set through events. No API support. Can be set through API.
Column Can be set through API. Can be set through API.
Cell Can be set through events. No API support. Can be set through API.
Others
Column span Not support. Can be set through API.
Row span Can be set through events. No API support. Can be set through API.
Nested table Can be set through events. No API support. Can be set through API.
Events BeginCellLayout,  EndCellLayout, BeginRowLayout, EndRowLayout, BeginPageLayout, EndPageLayout. BeginPageLayout, EndPageLayout.

The following sections demonstrate how to create a table in PDF using the PdfTable class and the PdfGrid class, respectively.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>10.3.4</version>
    </dependency>
</dependencies>
    

Create a Table in PDF Using PdfTable Class

The following are the steps to create a table using the PdfTable class using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Add a page to it using PdfDocument.getPages().add() method.
  • Create a Pdftable object.
  • Set the table style using the methods under PdfTableStyle object which is returned by PdfTable.getTableStyle() method.
  • Insert data to table using PdfTable.setDataSource() method.
  • Set row height and row color through BeginRowLayout event.
  • Draw table on the PDF page using PdfTable.draw() method.
  • Save the document to a PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.data.table.DataTable;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;
import com.spire.pdf.tables.*;

import java.awt.*;
import java.awt.geom.Point2D;

public class CreateTable {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Add a page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, new PdfMargins(40));

        //Create a PdfTable object
        PdfTable table = new PdfTable();

        //Set font for header and the rest cells
        table.getStyle().getDefaultStyle().setFont(new PdfTrueTypeFont(new Font("Times New Roman", Font.PLAIN, 12), true));
        table.getStyle().getHeaderStyle().setFont(new PdfTrueTypeFont(new Font("Times New Roman", Font.BOLD, 12), true));

        //Define data
        String[] data = {"ID;Name;Department;Position;Level",
                "1; David; IT; Manager; 1",
                "3; Julia; HR; Manager; 1",
                "4; Sophie; Marketing; Manager; 1",
                "7; Wickey; Marketing; Sales Rep; 2",
                "9; Wayne; HR; HR Supervisor; 2",
                "11; Mia; Dev; Developer; 2"};
        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]", -1);
        }

        //Set data as the table data
        table.setDataSource(dataSource);

        //Set the first row as header row
        table.getStyle().setHeaderSource(PdfHeaderSource.Rows);
        table.getStyle().setHeaderRowCount(1);

        //Show header(the header is hidden by default)
        table.getStyle().setShowHeader(true);

        //Set font color and background color of header row
        table.getStyle().getHeaderStyle().setBackgroundBrush(PdfBrushes.getGray());
        table.getStyle().getHeaderStyle().setTextBrush(PdfBrushes.getWhite());

        //Set text alignment in header row
        table.getStyle().getHeaderStyle().setStringFormat(new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));

        //Set text alignment in other cells
        for (int i = 0; i < table.getColumns().getCount(); i++) {
            table.getColumns().get(i).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle));
        }

        //Register with BeginRowLayout event
        table.beginRowLayout.add(new BeginRowLayoutEventHandler() {

            public void invoke(Object sender, BeginRowLayoutEventArgs args) {
                Table_BeginRowLayout(sender, args);

            }
        });

        //Draw table on the page
        table.draw(page, new Point2D.Float(0, 30));

        //Save the document to a PDF file
        doc.saveToFile("output/PdfTable.pdf");

    }

    //Event handler
    private static void Table_BeginRowLayout(Object sender, BeginRowLayoutEventArgs args) {

        //Set row height
        args.setMinimalHeight(20f);

        //Alternate color of rows except the header row
        if (args.getRowIndex() == 0) {
            return;
        }
        if (args.getRowIndex() % 2 == 0) {
            args.getCellStyle().setBackgroundBrush(PdfBrushes.getLightGray());
        } else {
            args.getCellStyle().setBackgroundBrush(PdfBrushes.getWhite());
        }
    }
}

Java: Create Tables in a PDF Document

Create a Table in PDF Using PdfGrid Class

Below are the steps to create a table in PDF using the PdfGrid class using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Add a page to it using PdfDocument.getPages().add() method.
  • Create a PdfGrid object.
  • Set the table style using the methods under the PdfGridStyle object which is returned by PdfGrid.getStyle() method.
  • Add rows and columns to the table using PdfGrid.getRows().add() method and PdfGrid.getColumns().add() method.
  • Insert data to specific cells using PdfGridCell.setValue() method.
  • Span cells across columns or rows using PdfGridCell.setRowSpan() method or PdfGridCell.setColumnSpan() method.
  • Set the formatting of a specific cell using PdfGridCell.setStringFormat() method and the methods under PdfGridCellStyle object.
  • Draw table on the PDF page using PdfGrid.draw() method.
  • Save the document to a PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.grid.PdfGrid;
import com.spire.pdf.grid.PdfGridRow;

import java.awt.*;
import java.awt.geom.Point2D;

public class CreateGrid {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Add a page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4,new PdfMargins(40));

        //Create a PdfGrid
        PdfGrid grid = new PdfGrid();

        //Set cell padding
        grid.getStyle().setCellPadding(new PdfPaddings(1, 1, 1, 1));

        //Set font
        grid.getStyle().setFont(new PdfTrueTypeFont(new Font("Times New Roman", Font.PLAIN, 13), true));

        //Add rows and columns
        PdfGridRow row1 = grid.getRows().add();
        PdfGridRow row2 = grid.getRows().add();
        PdfGridRow row3 = grid.getRows().add();
        PdfGridRow row4 = grid.getRows().add();
        grid.getColumns().add(4);

        //Set column width
        for (int i = 0; i < grid.getColumns().getCount(); i++) {

            grid.getColumns().get(i).setWidth(120);
        }

        //Write data into specific cells
        row1.getCells().get(0).setValue("Order and Payment Status");
        row2.getCells().get(0).setValue("Order number");
        row2.getCells().get(1).setValue("Date");
        row2.getCells().get(2).setValue ("Customer");
        row2.getCells().get(3).setValue("Paid or not");
        row3.getCells().get(0).setValue("00223");
        row3.getCells().get(1).setValue("2022/06/02");
        row3.getCells().get(2).setValue("Brick Lane Realty");
        row3.getCells().get(3).setValue("Yes");
        row4.getCells().get(0).setValue("00224");
        row4.getCells().get(1).setValue("2022/06/03");
        row4.getCells().get(3).setValue("No");

        //Span cell across columns
        row1.getCells().get(0).setColumnSpan(4);

        //Span cell across rows
        row3.getCells().get(2).setRowSpan(2);

        //Set text alignment of specific cells
        row1.getCells().get(0).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center));
        row3.getCells().get(2).setStringFormat(new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle));

        //Set background color of specific cells
        row1.getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getOrange());
        row4.getCells().get(3).getStyle().setBackgroundBrush(PdfBrushes.getLightGray());

        //Format cell border
        PdfBorders borders = new PdfBorders();
        borders.setAll(new PdfPen(new PdfRGBColor(Color.ORANGE), 0.8f));
        for (int i = 0; i < grid.getRows().getCapacity(); i++) {

            PdfGridRow gridRow = grid.getRows().get(i);
            gridRow.setHeight(20f);
            for (int j = 0; j < gridRow.getCells().getCount(); j++) {
                gridRow.getCells().get(j).getStyle().setBorders(borders);
            }

        }

        //Draw table on the page
        grid.draw(page, new Point2D.Float(0, 30));

        //Save the document to a PDF file
        doc.saveToFile("output/PdfGrid.pdf");
    }
}

Java: Create Tables in a PDF Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.