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 Vet_Visit_PDF_Report { SQL_Beef_Handler sql = new SQL_Beef_Handler(); String end = getDate() + " " + getTime(); private String FILE = "Reports/Beef Reports/Vet Visit 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<Vet_Visit_Data> vData; String cowtag; String info; String date; Double vet_cost = 0.0; public Vet_Visit_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("Vet Visit Overview Report"); document.addSubject("Vet Visit"); document.addKeywords("Beef, Vet Visit, 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("Vet Visit 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(5); 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("Cow Tag")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Info")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Date")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Vet Visit Cost")); 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) { Vet_Visit_Bar_Chart_Report chart = new Vet_Visit_Bar_Chart_Report(show, "Vet Visit 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) { Vet_Visit_Pie_Chart_Report chart = new Vet_Visit_Pie_Chart_Report(show, "Comparison", "Vet Visit 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 { vData = sql.select_vet_visit_data(); } catch (Exception e) { e.printStackTrace(); } } private void getExpenses(PdfPTable table) { //populating the tablemodel table.addCell("Vet Visit"); table.addCell(""); table.addCell(""); table.addCell(""); table.addCell(""); //populating the tablemodel ListIterator<Vet_Visit_Data> data_list = vData.listIterator(); while (data_list.hasNext()) { Vet_Visit_Data d = data_list.next(); cowtag = d.getCowTag(); info = d.getInfo(); date = d.getDate(); vet_cost = d.getExpense(); addToTable(table); } } private void addToTable(PdfPTable table) { table.addCell(""); table.addCell("" + cowtag); table.addCell("" + info); table.addCell("" + date); table.addCell("" + vet_cost); } }