report.pdfs.Slurry_PDF_Report.java Source code

Java tutorial

Introduction

Here is the source code for report.pdfs.Slurry_PDF_Report.java

Source

package report.pdfs;
/**
 * @author Thomas Donegan
 * @number R00044989
 * @e-mail thomas.donegan@mycit.ie
 * @version 0.0.1
 */

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ListIterator;
import java.util.TimeZone;

import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import financemanager.datahandler.*;
import financemanager.sqlhandler.SQL_Handler;
import report.barcharts.*;
import report.piecharts.*;

public class Slurry_PDF_Report {
    SQL_Handler sql = new SQL_Handler();

    String end = getDate() + " " + getTime();
    private String FILE = "Reports/Basic Reports/Slurry Reports/" + end + ".pdf";
    private Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
    private Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED);
    private Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
    private Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);

    java.util.List<Slurry_Data> sData;

    boolean show = false;

    public Slurry_PDF_Report() {
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();
            addMetaData(document);
            addTitlePage(document);
            addContent(document);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // iText allows to add metadata to the PDF which can be viewed in your Adobe
    // Reader
    // under File -> Properties
    private void addMetaData(Document document) {
        document.addTitle("Slurry Overview Report");
        document.addSubject("Slurry");
        document.addKeywords("Basics, Slurry, Report");
        document.addAuthor("admin");
        document.addCreator(System.getProperty("user.name"));
    }

    private void addTitlePage(Document document) throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        preface.add(new Paragraph("Slurry Overview", catFont));

        addEmptyLine(preface, 1);
        // Will create: Report generated by: _name, _date
        preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-3$
                smallBold));
        addEmptyLine(preface, 3);
        preface.add(new Paragraph("This document describes something which is very important ", smallBold));

        addEmptyLine(preface, 8);

        preface.add(new Paragraph("This document is a preliminary version and not subject to "
                + "your license agreement or any other agreement with vogella.com ;-).", redFont));

        document.add(preface);
        // Start a new page
        document.newPage();
    }

    private void addContent(Document document) throws DocumentException {
        Anchor anchor = new Anchor("Table Report", catFont);
        anchor.setName("Table Report");

        // Second parameter is the number of the chapter
        Chapter catPart = new Chapter(new Paragraph(anchor), 1);

        Paragraph paragraph = new Paragraph();

        // Add a table
        document.add(new Paragraph());
        createTable(catPart);

        // Now add all this to the document
        document.add(catPart);

        // Start a new page
        document.newPage();

        // Next section
        anchor = new Anchor("Second Chapter", catFont);
        anchor.setName("Second Chapter");

        // Second parameter is the number of the chapter
        catPart = new Chapter(new Paragraph(anchor), 2);

        // Add an image
        addEmptyLine(paragraph, 5);
        addBarChart(catPart);

        // Now add all this to the document
        document.add(catPart);

        // Start a new page
        document.newPage();

        // Next section
        anchor = new Anchor("Third Chapter", catFont);
        anchor.setName("Third Chapter");

        // Second parameter is the number of the chapter
        catPart = new Chapter(new Paragraph(anchor), 3);

        // Add an image
        addEmptyLine(paragraph, 5);
        addPieChart(catPart);

        // Now add all this to the document
        document.add(catPart);
    }

    private void createTable(Section catPart) throws BadElementException {
        PdfPTable table = new PdfPTable(4);
        table.setSpacingBefore(25);
        // t.setBorderColor(BaseColor.GRAY);
        // t.setPadding(4);
        // t.setSpacing(4);
        // t.setBorderWidth(1);

        PdfPCell c1 = new PdfPCell(new Phrase("Type"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Expense"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Month"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Year"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        table.setHeaderRows(1);

        selectAll();
        calcSExpense(table);

        catPart.add(table);
    }

    private void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    private void addBarChart(Section catPart) {
        Slurry_Bar_Chart_Report chart = new Slurry_Bar_Chart_Report(show, "Slurry Expenses Overview");
        Image To_be_Added = null;
        try {
            To_be_Added = Image.getInstance("bar_chart.png");
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        To_be_Added.setAlignment(Image.MIDDLE | Image.TEXTWRAP);
        //To_be_Added.setBorder(Image.BOX);
        //To_be_Added.setBorderWidth(15);

        catPart.add(To_be_Added);
    }

    private void addPieChart(Section catPart) {
        Slurry_Pie_Chart_Report chart = new Slurry_Pie_Chart_Report(show, "Comparison", "Slurry Expenses Overview");
        Image To_be_Added = null;
        try {
            To_be_Added = Image.getInstance("pie_chart.png");
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        To_be_Added.setAlignment(Image.MIDDLE | Image.TEXTWRAP);
        //To_be_Added.setBorder(Image.BOX);
        //To_be_Added.setBorderWidth(15);

        catPart.add(To_be_Added);
    }

    private final static String getDate() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return (df.format(new Date()));
    }

    private final static String getTime() {
        DateFormat df = new SimpleDateFormat("hh-mm-ss");
        df.setTimeZone(TimeZone.getTimeZone("Ireland"));

        return (df.format(new Date()));
    }

    private void selectAll() {
        try {
            sData = sql.select_slurry_expense();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void calcSExpense(PdfPTable table) {
        ListIterator<Slurry_Data> data_list = sData.listIterator();
        //populating the tablemodel
        int i = 0;
        while (data_list.hasNext()) {
            Slurry_Data d = data_list.next();
            if (i == 0) {
                table.addCell("Slurry");
                table.addCell("");
                table.addCell("");
                table.addCell("");
                i++;
            } else {
                table.addCell("");
                table.addCell("" + d.getExpense());
                table.addCell(d.getMonth());
                table.addCell("" + d.getYear());
            }
        }
    }
}