Java add a table in a PDF

This article describes how to add a table in a PDF document by Java programming. When adding a table, the table may be provided borders, cell alignment, the background color of the cell, cell merging, insert a picture, set high row, column width, font, font size.

 

Use tools: as Free Spire.PDF for the Java (free version)

Jar file acquisition and import:

Method 1 : through the official website to download jar package. After the download, unzip the file, the lib folder under the Spire.Pdf.jar files into Java programs.

 

Method 2 : By mounting introducing maven repository .

 

Java code examples

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

public class AddTable {
    public static void main(String[]args){
        // create a document, add PDF pages
        PdfDocument pdf = new PdfDocument();
        PdfPageBase page = pdf.getPages().add();

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

        // Set the margins within a cell, the default font, font color and background color default
        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());

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

        // define data
        String [] data = { "school; Logo; Colleges codes; institutions address",
                "Sichuan University ;; 10610; Chengdu"
                "Southwest Jiaotong University ;; 10613; Chengdu"
                "University of Electronic Science and Technology ;; 10614; Chengdu"
                "Southwest Petroleum University ;; 10615; Chengdu"
        };
        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]");
        }

        // populate the data to a table
        grid.setDataSource(dataSource);

        // fill in Form 2 picture and set the column width
        grid.getRows().get(1).getCells().get(1).getStyle().setBackgroundImage(PdfImage.fromFile("scdx.png"));
        grid.getRows().get(2).getCells().get(1).getStyle().setBackgroundImage(PdfImage.fromFile("xnjtdx.png"));
        grid.getRows().get(3).getCells().get(1).getStyle().setBackgroundImage(PdfImage.fromFile("dzkjdx.png"));
        grid.getRows().get(4).getCells().get(1).getStyle().setBackgroundImage(PdfImage.fromFile("xnsydx.png"));
        grid.getColumns().get(1).setWidth(100f);

        // vertical merges
        grid.getRows().get(1).getCells().get(3).setRowSpan(4);

        // set the table
        for (int i = 0; i < data.length ; i++) {
            // set the height of each row
            grid.getRows().get(i).setHeight(50f);
            // set the first column of fonts
            grid.getRows().get(i).getCells().get(0).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true));

            for(int j =0;j<grid.getColumns().getCount();j++){
                // set all cells in the middle
                grid.getRows().get(i).getCells().get(j).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle));
                // set the background color of the first line
                grid.getRows().get(0).getCells().get(j).getStyle().setBackgroundBrush(PdfBrushes.getBeige());
            }
        }

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

        // save the document
        pdf.saveToFile ( "Add Form .pdf");
        pdf.close();
    }
}

 

Form add effects:

 


 (This article End)

 

Guess you like

Origin miaonly.iteye.com/blog/2442409