Java tutorial
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.List; import java.util.ListIterator; import java.util.TimeZone; import beefmodule.datahandler.*; import beefmodule.sqlhandler.SQL_Beef_Handler; 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 report.barcharts.*; import report.piecharts.*; public class Herd_PDF_Report { SQL_Beef_Handler sql = new SQL_Beef_Handler(); String end = getDate() + " " + getTime(); private String FILE = "Reports/Beef Reports/Herd 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); boolean show = false; List<Herd_Data> hData; List<Feed_Data> fData; List<Medical_Data> mData; List<Vet_Visit_Data> vData; List<CowTag_Data> cData; int herd_size = 0; Double feed_cost = 0.0; Double med_cost = 0.0; Double vet_cost = 0.0; Double total = 0.0; public Herd_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("Herd Overview Report"); document.addSubject("Herd"); document.addKeywords("Beef, Herd, 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("Herd 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(6); 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("Herd ID")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Feed")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Medical")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Vet Visits")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Total")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); table.setHeaderRows(1); selectAll(); getExpenses(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) { Herd_Bar_Chart_Report chart = new Herd_Bar_Chart_Report(show, "Herd 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) { Herd_Pie_Chart_Report chart = new Herd_Pie_Chart_Report(show, "Comparison", "Herd 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 { hData = sql.select_herd_data(); fData = sql.select_feed_data(); mData = sql.select_medical_data(); vData = sql.select_vet_visit_data(); cData = sql.select_herd_linker_data(); } catch (Exception e) { e.printStackTrace(); } } private void getExpenses(PdfPTable table) { //populating the tablemodel table.addCell("Herd"); table.addCell(""); table.addCell(""); table.addCell(""); table.addCell(""); table.addCell(""); int i = 0; int size = hData.size(); boolean check = false; while (i < size) { ListIterator<Herd_Data> data_list = hData.listIterator(); while (data_list.hasNext()) { Herd_Data d = data_list.next(); if (i == d.getHerd_id()) { check = true; getFeedExpense(i); String cowtag = getCowTag(i); getMedExpense(cowtag); getVetExpense(cowtag); herd_size++; } } total += feed_cost; total += med_cost; total += vet_cost; if (check == true) { addToTable(i, table); } herd_size = 0; feed_cost = 0.0; med_cost = 0.0; vet_cost = 0.0; total = 0.0; i++; } } private void getFeedExpense(int i) { ListIterator<Feed_Data> data_list = fData.listIterator(); while (data_list.hasNext()) { Feed_Data d = data_list.next(); if (i == d.getHerd_id()) { feed_cost += d.getF_hay_expense(); feed_cost += d.getF_nuts_expense(); feed_cost += d.getF_silage_expense(); } } } private String getCowTag(int i) { ListIterator<CowTag_Data> data_list = cData.listIterator(); while (data_list.hasNext()) { CowTag_Data d = data_list.next(); if (i == d.getHerd_id()) { return d.getCowTag(); } } return ""; } private void getMedExpense(String cowTag) { ListIterator<Medical_Data> data_list = mData.listIterator(); while (data_list.hasNext()) { Medical_Data d = data_list.next(); if (cowTag.equals(d.getCowTag())) { med_cost += d.getExpense(); } } } private void getVetExpense(String cowTag) { ListIterator<Vet_Visit_Data> data_list = vData.listIterator(); while (data_list.hasNext()) { Vet_Visit_Data d = data_list.next(); if (cowTag.equals(d.getCowTag())) { vet_cost += d.getExpense(); } } } private void addToTable(int i, PdfPTable table) { table.addCell(""); table.addCell("" + i); table.addCell("" + feed_cost); table.addCell("" + med_cost); table.addCell("" + vet_cost); table.addCell("" + total); } }