package nl.itopia.corendon.pdf; import java.io.FileOutputStream; import java.util.Date; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import java.awt.Desktop; import java.io.File; import java.io.IOException; import nl.itopia.corendon.data.Customer; import nl.itopia.corendon.data.Luggage; import nl.itopia.corendon.model.EmployeeModel; /** * * @author Igor's_Boven */ public class LuggageResolvedPDF { private static Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); public static void generateLuggageResolvedPDF(File file, Luggage luggage, Customer customer){ try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); addContent(file, document, luggage, customer); document.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addContent(File file, Document document, Luggage luggage, Customer customer) throws DocumentException { Paragraph paragraph = new Paragraph(); addEmptyLine(paragraph, 1); // TITLE paragraph.add(new Paragraph("Luggage resolved report", titleFont)); String username = (EmployeeModel.getDefault().currentEmployee.username); addEmptyLine(paragraph, 1); paragraph.add(new Paragraph("Report generated by: " + username + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ smallBold)); addEmptyLine(paragraph, 3); paragraph.add(new Paragraph("This document is a report of a resolved luggage case. It contains info of the resolved luggage and the associated customer.", smallBold)); addEmptyLine(paragraph, 1); //generate table with the data of luggage in the PDF PdfPTable table = new PdfPTable(2); PdfPCell c1 = new PdfPCell(new Phrase("Datatype")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); PdfPCell c2 = new PdfPCell(new Phrase("Value")); c2.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c2); table.addCell("Color"); table.addCell(luggage.color.getHex()); table.addCell("Status"); table.addCell(luggage.status.getName()); table.addCell("Employee"); table.addCell(luggage.employee.firstName + " " + luggage.employee.lastName); table.addCell("Airport"); table.addCell(luggage.airport.getName()); table.addCell("Dimensions"); table.addCell(luggage.dimensions); table.addCell("Label"); table.addCell(luggage.label); table.addCell("Notes"); table.addCell(luggage.notes); table.addCell("Weight"); table.addCell(luggage.weight); table.addCell("Brand"); table.addCell(luggage.brand.getName()); //generate customer table Paragraph emptylines = new Paragraph(); addEmptyLine(emptylines, 3); PdfPTable table2 = new PdfPTable(2); table2.addCell(c1); table2.addCell(c2); table2.addCell("First name"); table2.addCell(customer.firstName); table2.addCell("Last name"); table2.addCell(customer.lastName); table2.addCell("Address"); table2.addCell(customer.address); table2.addCell("Zipcode"); table2.addCell(customer.zipcode); table2.addCell("State"); table2.addCell(customer.state); // table2.addCell("Country"); // table2.addCell(customer.country.name); table2.addCell("Email"); table2.addCell(customer.email); table2.addCell("Phonenumber"); table2.addCell(customer.phone); document.add(paragraph); document.add(table); document.add(emptylines); document.add(table2); try { Desktop.getDesktop().open(file); } catch (IOException ex) { System.out.println("You need to have default program set to open .PDF files."); } } private static void addEmptyLine(Paragraph paragraph, int lines) { for (int i = 0; i < lines; i++) { paragraph.add(new Paragraph(" ")); } } }