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.Luggage; import nl.itopia.corendon.model.EmployeeModel; /** * * @author Igor's_Boven */ public class LuggageReportPDF { 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 generateLuggageReportPDF(File file, Luggage luggage){ try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); addContent(file, document, luggage); document.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addContent(File file, Document document, Luggage luggage) throws DocumentException { Paragraph paragraph = new Paragraph(); addEmptyLine(paragraph, 1); // TITLE paragraph.add(new Paragraph("Luggage 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 lost or found luggage case.", smallBold)); addEmptyLine(paragraph, 4); //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()); document.add(paragraph); document.add(table); 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(" ")); } } }