Table

Table (2)

Wednesday, 12 December 2018 03:22

Create Grid in PDF in Java

Written by support iceblue

In this article, you'll learn how to add a grid to PDF and how to format the grid as well, by using Spire.PDF with Java.

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 pdf document
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();

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

        //set the cell padding, font, text brush, background brush of the grid
        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
        PdfBorders borders= new PdfBorders();
        borders.setAll(new PdfPen(PdfBrushes.getWhite(),1f));

        //define sample data
        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 grid with data
        grid.setDataSource(dataSource);

        //fill the cells with background images
        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 width of the last column
        grid.getColumns().get(grid.getColumns().getCount()-1).setWidth(60f);

        //vertically span cells
        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 of each row
            grid.getRows().get(i).setHeight(30f);
            //set the background color of the first column
            grid.getRows().get(i).getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());
            //set the font of 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++) {

                //apply border style to all cells
                grid.getRows().get(i).getCells().get(j).getStyle().setBorders(borders);
                //apply text alignment to all cells
                grid.getRows().get(i).getCells().get(j).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle));
                //set the font of the first row
                grid.getRows().get(0).getCells().get(j).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));
                //set the background color of the first row
                grid.getRows().get(0).getCells().get(j).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());

            }
        }

        //draw grid on the pdf page
        grid.draw(page,0,30);

        //save to file
        doc.saveToFile("Grid.pdf");
        doc.close();
    }
}

Create Grid in PDF in Java

Wednesday, 14 November 2018 07:17

Create Table in PDF in Java

Written by support iceblue

This article is going to show you how to create a table in a PDF file using Spire.PDF for Java.

import com.spire.pdf.*;
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 instance
        PdfDocument doc = new PdfDocument();

        //Set margins
        PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
        PdfMargins margin = new PdfMargins();
        margin.setTop(unitCvtr.convertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
        margin.setBottom(margin.getTop());
        margin.setLeft(unitCvtr.convertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
        margin.setRight(margin.getLeft());

        //Add one page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);

        float y = 10;

        //Draw title
        PdfBrush brush1 = PdfBrushes.getBlack();
        PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial",Font.BOLD ,16));
        PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
        page.getCanvas().drawString("Country List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
        y = y + (float) font1.measureString("Country List", format1).getHeight();
        y = y + 5;

        //Data source to create table
        String[] data = {"Name;Capital;Continent;Area;Population", "Argentina;Buenos Aires;South America;2777815;32300003", "Bolivia;La Paz;South America;1098575;7300000", "Brazil;Brasilia;South America;8511196;150400000", "Canada;Ottawa;North America;9976147;26500000", "Chile;Santiago;South America;756943;13200000", "Colombia;Bagota;South America;1138907;33000000", "Cuba;Havana;North America;114524;10600000", "Ecuador;Quito;South America;455502;10600000", "El Salvador;San Salvador;North America;20865;5300000", "Guyana;Georgetown;South America;214969;800000", "Jamaica;Kingston;North America;11424;2500000", "Mexico;Mexico City;North America;1967180;88600000", "Nicaragua;Managua;North America;139000;3900000", "Paraguay;Asuncion;South America;406576;4660000", "Peru;Lima;South America;1285215;21600000", "United States of America;Washington;North America;9363130;249200000", "Uruguay;Montevideo;South America;176140;3002000", "Venezuela;Caracas;South America;912047;19700000"};

        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]", -1);
        }

        //Create a PdfTable instance and set data source
        PdfTable table = new PdfTable();
        table.getStyle().setCellPadding(2);
        table.getStyle().setHeaderSource(PdfHeaderSource.Rows);
        table.getStyle().setHeaderRowCount(1);
        table.getStyle().setShowHeader(true);
        table.setDataSource(dataSource);

        //Draw table to the page
        PdfLayoutResult result = table.draw(page, new Point2D.Float(0, y));

        y = y + (float) result.getBounds().getHeight() + 5;

        //Draw string below table
        PdfBrush brush2 = PdfBrushes.getGray();
        PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 0,9));
        page.getCanvas().drawString(String.format("* %1$s countries in the list.", data.length - 1), font2, brush2, 5, y);

        //Save the file
        doc.saveToFile("output/Table.pdf");
    }
}

Output:

Create Table in PDF in Java