Java adds table to PDF

The text will show you how to add a table to a PDF document through Java. When you add a table, you can set the table border, cell placement, cell background color, cell merging, image insertion, row height, column width, font, size, and more.

Tools used: Free Spire.PDF for Java (Free version)

Getting and installing Jar files:

Method 1: Download the jar file bag through the homepage. After downloading, unzip the file and turn the Spire.Pdf.jar file under the lib folder into a Java program. Introduce.

Method 2: Maven Warehouse Introduction by installation.

import com.spire.pdf.graphics.*;
import com.spire.pdf.grid.PdfGrid;
import java.awt.*;

public class CreateGrid {

    public static void main(String[] args) {
        //Create a document and add a PDF page
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();

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

        //Set in-cell margins, standard font, font color, standard background color
        grid.getStyle().setCellPadding(new PdfPaddings(3,3,3,3));
        grid.getStyle().setFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN,10), true));
        grid.getStyle().setTextBrush(PdfBrushes.getBlack());
        grid.getStyle().setBackgroundBrush(PdfBrushes.getLightGray());

        //Create a PdfBorders object and set the color and thickness
        PdfBorders borders= new PdfBorders();
        borders.setAll(new PdfPen(PdfBrushes.getWhite(),1f));

        //Data definition
        String[] data = {"Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; "
                };
        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]", -1);
        }

        //Fill the table with data
        grid.setDataSource(dataSource);

        //Set cell background color grid.getRows().get(1).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-China.png "));
        grid.getRows().get(2).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Japan.png "));
        grid.getRows().get(3).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-United-Kingdom.png "));
        grid.getRows().get(4).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Germany.png "));
        grid.getRows().get(5).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Canada.png "));
        grid.getRows().get(6).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-United-States-of-America.png "));

        //Set the height for each row
        grid.getColumns().get(grid.getColumns().getCount()-1).setWidth(60f);

        //Combine cells vertically
        grid.getRows().get(1).getCells().get(0).setRowSpan(2);
        grid.getRows().get(3).getCells().get(0).setRowSpan(2);
        grid.getRows().get(5).getCells().get(0).setRowSpan(2);

        for (int i = 0; i < data.length ; i++) {

            //Set the height for each column
            grid.getRows().get(i).setHeight(30f);

            //Sets the background color for the first column grid.getRows().get(i).getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());
            //Set the font for the first column
grid.getRows().get(i).getCells().get(0).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));

            for (int j = 0; j < grid.getColumns().getCount(); j++) {

                //Sets the border style for all cells
grid.getRows().get(i).getCells().get(j).getStyle().setBorders(borders);

                //Sets the alignment of text in all cells
grid.getRows().get(i).getCells().get(j).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle));

                //Set the font for the first line
grid.getRows().get(0).getCells().get(j).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));

                //Sets the background color of the first line
grid.getRows().get(0).getCells().get(j).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());

            }
        }

        //Draw the table to PDF
        grid.draw(page,0,30);

        //Save document
        doc.saveToFile("Grid.pdf");
        doc.close();
    }
}

PDF table.png

Recommended Posts

Java adds table to PDF
Java adds form fields to PDF
Java adds page numbers to existing PDF documents
Java to extract PDF text content
Java adds SmartArt graphics to PowerPoint
Java adds hyperlinks to Word documents
Add watermark to Java to PDF document
[Java] Introduction to Java
Introduction to java
Java adds a text box to PowerPoint slides
I tried to output multiplication table in Java
[Java] Convert PDF version
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Java compressed PDF document
[Java] Connect to MySQL
Kotlin's improvements to Java
[Java] PDF viewing settings
From Java to Ruby !!
Introduction to java command
Java table expression injection
Launch Docker from Java to convert Office documents to PDF
Java applications convert Word (DOC / DOCX) documents to PDF
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
How to uninstall Java 8 (Mac)
Java to play with Function
Save Java PDF in Excel
Java --How to make JTable
How to use java Optional
Java encryption and decryption PDF
New features from Java7 to Java8
How to minimize Java images
How to write java comments
How to use java class
Paging PDF with Java + PDFBox.jar
[Java] How to use Optional ②
Connect from Java to PostgreSQL
[Java] How to use removeAll ()
[Java] How to display Wingdings
Java turns Excel into PDF
Save Java HTML as PDF
[Java] Introduction to lambda expressions
[Java] How to use string.format
Shell to kill Java process
How to use Java Map
How to set Java constants
Connect to DB with Java
Connect to MySQL 8 with Java
[java] Reasons to use static
How to use Java variables
[Java] Introduction to Stream API
Java8 to start now ~ Optional ~
How to convert Java radix
[Java] Convert ArrayList to array
Java thread to understand loosely
[Java] How to implement multithreading
[Java] How to use Optional ①
From Ineffective Java to Effective Java
How to initialize Java array