Using the Table class for reports creation

Example_08.pdf
import java.lang.*;
import java.io.*;
import java.util.*;

import com.pdfjet.*;


/**
 *  Example_08.java
 *
 */
public class Example_08 {

    public Example_08() throws Exception {

        FileOutputStream fos = new FileOutputStream("Example_08.pdf");

        PDF pdf = new PDF(fos, Compliance.PDF_A_1B);

        Font f1 = new Font(pdf,
                new BufferedInputStream(getClass().getResourceAsStream(
                        "/fonts/DroidFonts/DroidSerif-Bold.otf")),
                CodePage.UNICODE,
                Embed.YES);
        f1.setSize(7.0);

        Font f2 = new Font(pdf,
                new BufferedInputStream(getClass().getResourceAsStream(
                        "/fonts/DroidFonts/DroidSerif-Regular.otf")),
                CodePage.UNICODE,
                Embed.YES);
        f2.setSize(7.0);

        Font f3 = new Font(pdf,
                new BufferedInputStream(getClass().getResourceAsStream(
                        "/fonts/DroidFonts/DroidSerif-Italic.otf")),
                CodePage.UNICODE,
                Embed.YES);
        f3.setSize(7.0);

        Page page = new Page(pdf, Letter.PORTRAIT);

        Table table = new Table(f1, f2);
        List> tableData = getData(
                "data/world-communications.txt", "|", Table.DATA_HAS_2_HEADER_ROWS, f1, f2);
        table.setData(tableData, Table.DATA_HAS_2_HEADER_ROWS);
        
        table.setLineWidth(0.2);
        table.setPosition(70.0, 30.0);
        table.setTextColorInRow(6, RGB.BLUE);
        table.setTextColorInRow(39, RGB.RED);
        table.setTextFontInRow(26, f3, 7);
        table.removeLineBetweenRows(0, 1);  
        table.autoAdjustColumnWidths();
        table.setColumnWidth(0, 120);
        table.rightAlignNumbers();
        int numOfPages = table.getNumberOfPages(page);
        while (true) {
            Point point = table.drawOn(page);
            // System.out.println(table.getRowsRendered());
            // System.out.println(point.getX() + " " + point.getY());
            // TO DO: Draw "Page 1 of N" here
            if (!table.hasMoreData()) break;
            page = new Page(pdf, Letter.PORTRAIT);
        }

        pdf.flush();
        fos.close();
    }
    
    
    public List> getData(
            String fileName,
            String delimiter,
            int numOfHeaderRows,
            Font f1,
            Font f2) throws Exception {

        List> tableData = new ArrayList>();

        int currentRow = 0;
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        String line = null;
        while ((line = reader.readLine()) != null) {
            List row = new ArrayList();
            String[] cols = null;
            if (delimiter.equals("|")) {
                cols = line.split("\\|", -1);
            } else if (delimiter.equals("\t")) {
                cols = line.split("\t", -1);
            } else {
                throw new Exception(
                        "Only pipes and tabs can be used as delimiters");
            }
            for (int i = 0; i < cols.length; i++) {
                Cell cell = new Cell(f2, cols[i].trim());
                if (currentRow < numOfHeaderRows) {
                    cell.setFont(f1);
                }
                row.add(cell);
            }
            tableData.add(row);
            currentRow++;
        }
        reader.close();

        appendMissingCells(tableData, f2);
        
        return tableData;
    }
    

    private void appendMissingCells(List> tableData, Font f2) {
        List firstRow = tableData.get(0);
        int numOfColumns = firstRow.size();
        for (int i = 0; i < tableData.size(); i++) {
            List dataRow = tableData.get(i);
            int dataRowColumns = dataRow.size();
            if (dataRowColumns < numOfColumns) {
                for (int j = 0; j < (numOfColumns - dataRowColumns); j++) {
                    dataRow.add(new Cell(f2));
                }
                dataRow.get(dataRowColumns - 1).setColspan((numOfColumns - dataRowColumns) + 1);
            }
        }
    }


    public static void main(String[] args) {
        try {
            new Example_08();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}   // End of Example_08.java

© 2011 Innovatics Inc.