List of usage examples for com.lowagie.text Document addSubject
public boolean addSubject(String subject)
From source file:com.exam.server.ConvertPDF.java
private static void addMetaData(Document document) { document.addTitle("My first PDF"); document.addSubject("Using iText"); document.addKeywords("Java, PDF, iText"); document.addAuthor("Lars Vogel"); document.addCreator("Lars Vogel"); }
From source file:com.googlecode.openmpis.action.AbductorAction.java
License:Open Source License
/** * Prints the abductor's poster in PDF file. * * @param mapping the ActionMapping used to select this instance * @param form the optional ActionForm bean for this request * @param request the HTTP Request we are processing * @param response the HTTP Response we are processing * @return the forwarding instance * @throws java.lang.Exception/* w w w . j a v a2 s .c o m*/ */ public ActionForward printPoster(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Set the paper size and margins Document document = new Document(PageSize.LETTER, 50, 50, 50, 50); // Create the PDF writer ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); // Retrieve the abductor try { int id = Integer.parseInt(request.getParameter("id")); Abductor abductor = abductorService.getAbductorById(id); // Process the photo String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/") + "photo" + File.separator + "unknown.png"; if (abductor.getPhoto() != null) { String tokens[] = abductor.getPhoto().split("\\/"); String defaultPhotoBasename = ""; for (int i = 0; i < tokens.length - 1; i++) { defaultPhotoBasename += tokens[i] + File.separator; } defaultPhotoBasename += tokens[tokens.length - 1]; absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/") + defaultPhotoBasename; } // Add some meta information to the document document.addTitle("Poster"); document.addAuthor("OpenMPIS"); document.addSubject("Poster for " + abductor.getNickname()); document.addKeywords("OpenMPIS, missing, found, unidentified"); document.addProducer(); document.addCreationDate(); document.addCreator("OpenMPIS version " + Constants.VERSION); // Open the document for writing document.open(); // Add the banner Paragraph wantedParagraph = new Paragraph("W A N T E D", FontFactory.getFont(FontFactory.HELVETICA_BOLD, 36, Font.BOLD, new Color(255, 0, 0))); wantedParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(wantedParagraph); // Add name Paragraph redParagraph; if (!abductor.getNickname().isEmpty()) { redParagraph = new Paragraph( abductor.getFirstName() + " \"" + abductor.getNickname() + "\" " + abductor.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); } else { redParagraph = new Paragraph(abductor.getFirstName() + " " + abductor.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); } redParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(redParagraph); // Add the photo Image image = Image.getInstance(absoluteDefaultPhotoFilename); image.scaleAbsolute(200, 300); image.setAlignment(Image.ALIGN_CENTER); document.add(image); // Add birth date Paragraph blackParagraph; if (abductor.getBirthMonth() > 0) { blackParagraph = new Paragraph( getResources(request).getMessage("label.date.birth") + ": " + getResources(request).getMessage("month." + abductor.getBirthMonth()) + " " + abductor.getBirthDay() + ", " + abductor.getBirthYear(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); } // Add birth place blackParagraph = new Paragraph( getResources(request).getMessage("label.address.city") + ": " + abductor.getCity(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add sex blackParagraph = new Paragraph( getResources(request).getMessage("label.sex") + ": " + getResources(request).getMessage("sex." + abductor.getSex()), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add height blackParagraph = new Paragraph( getResources(request).getMessage("label.height") + ": " + abductor.getFeet() + "' " + abductor.getInches() + "\"", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add weight blackParagraph = new Paragraph( getResources(request).getMessage("label.weight") + ": " + abductor.getWeight() + " " + getResources(request).getMessage("label.weight.lbs"), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add hair color blackParagraph = new Paragraph( getResources(request).getMessage("label.color.hair") + ": " + getResources(request).getMessage("color.hair." + abductor.getHairColor()), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add eye color blackParagraph = new Paragraph( getResources(request).getMessage("label.color.eye") + ": " + getResources(request).getMessage("color.eye." + abductor.getEyeColor()), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add race blackParagraph = new Paragraph( getResources(request).getMessage("label.race") + ": " + getResources(request).getMessage("race." + abductor.getRace()), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add circumstance blackParagraph = new Paragraph( getResources(request).getMessage("label.remarks") + ": " + abductor.getRemarks(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add line blackParagraph = new Paragraph("---------------------------------------"); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add contact blackParagraph = new Paragraph(getResources(request).getMessage("global.contact"), FontFactory.getFont(FontFactory.HELVETICA, 14, Font.NORMAL, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); document.close(); // Set the response to return the poster (PDF file) response.setContentType("application/pdf"); response.setContentLength(baos.size()); response.setHeader("Content-disposition", "attachment; filename=Poster.pdf"); // Close the output stream baos.writeTo(response.getOutputStream()); response.getOutputStream().flush(); return null; } catch (NumberFormatException nfe) { return mapping.findForward(Constants.LIST_PERSON); } catch (NullPointerException npe) { return mapping.findForward(Constants.LIST_PERSON); } }
From source file:com.googlecode.openmpis.action.CaseAction.java
License:Open Source License
/** * Writes the cases to a PDF file.//www .j av a2s .c om * * @param mapping the ActionMapping used to select this instance * @param form the optional ActionForm bean for this request * @param request the HTTP Request we are processing * @param response the HTTP Response we are processing * @return the forwarding instance * @throws java.lang.Exception */ public ActionForward printCases(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { User currentUser = null; // Check if there exists a session if (request.getSession().getAttribute("currentuser") != null) { currentUser = (User) request.getSession().getAttribute("currentuser"); } // Set the paper size and margins Document document = new Document(PageSize.LETTER.rotate(), 50, 50, 50, 50); // Create the PDF writer ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); // Add some meta information to the document document.addTitle("Case Statistics"); document.addAuthor("OpenMPIS"); document.addSubject("Statistics for All Cases"); document.addKeywords("OpenMPIS, missing, found, unidentified"); document.addProducer(); document.addCreationDate(); document.addCreator("OpenMPIS version " + Constants.VERSION); // Set the header String date = simpleDateFormat.format(System.currentTimeMillis()); document.setHeader(new HeaderFooter(new Phrase("Statistics for cases as of " + date), false)); // Set the footer HeaderFooter footer = new HeaderFooter(new Phrase("Page : "), true); footer.setAlignment(Element.ALIGN_CENTER); document.setFooter(footer); // Open the document for writing document.open(); Table table = new Table(2); table.setBorderWidth(1); table.setBorderColor(new Color(0, 0, 0)); table.setPadding(2); table.setSpacing(0); Paragraph paragraph = new Paragraph("Cases", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD, new Color(0, 0, 0))); paragraph.setAlignment(Paragraph.ALIGN_CENTER); Cell cell = new Cell(paragraph); cell.setHeader(true); cell.setColspan(2); table.addCell(cell); table.endHeaders(); table.addCell("Total On-going Cases"); table.addCell("" + personService.countOngoing()); table.addCell("\t\t\t\t\tMissing Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countMissing()); table.addCell("\t\t\t\t\tFamily Abductions"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countFamilyAbduction()); table.addCell("\t\t\t\t\tNon-Family Abductions"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countNonFamilyAbduction()); table.addCell("\t\t\t\t\tRunaway Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countRunaway()); table.addCell("\t\t\t\t\tUnknown"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnknown()); table.addCell("\t\t\t\t\tFound Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countFound()); table.addCell("\t\t\t\t\tAbandoned Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countAbandoned()); table.addCell("\t\t\t\t\tThrowaway Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countThrowaway()); table.addCell("\t\t\t\t\tUnidentified"); table.addCell( "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnidentified()); table.addCell("Total Solved Cases"); table.addCell("" + personService.countSolved()); table.addCell("\t\t\t\t\tMissing Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedMissing()); table.addCell("\t\t\t\t\tFamily Abductions"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedFamilyAbduction()); table.addCell("\t\t\t\t\tNon-Family Abductions"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedNonFamilyAbduction()); table.addCell("\t\t\t\t\tRunaway Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedRunaway()); table.addCell("\t\t\t\t\tUnknown"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedUnknown()); table.addCell("\t\t\t\t\tFound Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedFound()); table.addCell("\t\t\t\t\tAbandoned Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedAbandoned()); table.addCell("\t\t\t\t\tThrowaway Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedThrowaway()); table.addCell("\t\t\t\t\tUnidentified"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countSolvedUnidentified()); table.addCell("Total Unsolved Cases"); table.addCell("" + personService.countUnsolved()); table.addCell("\t\t\t\t\tMissing Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedMissing()); table.addCell("\t\t\t\t\tFamily Abductions"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedFamilyAbduction()); table.addCell("\t\t\t\t\tNon-Family Abductions"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedNonFamilyAbduction()); table.addCell("\t\t\t\t\tRunaway Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedRunaway()); table.addCell("\t\t\t\t\tUnknown"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedUnknown()); table.addCell("\t\t\t\t\tFound Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedFound()); table.addCell("\t\t\t\t\tAbandoned Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedAbandoned()); table.addCell("\t\t\t\t\tThrowaway Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedThrowaway()); table.addCell("\t\t\t\t\tUnidentified"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnsolvedUnidentified()); table.addCell("Total Cases"); table.addCell("" + personService.countAllPersons()); table.addCell("\t\t\t\t\tTotal Missing Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + personService.countAllMissing()); table.addCell("\t\t\t\t\tTotal Found Persons"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countAllFound()); table.addCell("\t\t\t\t\tTotal Unidentified Persons"); table.addCell( "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + personService.countUnidentified()); table.addCell("Total Relatives"); table.addCell("" + relativeService.countAllRelatives()); table.addCell("Total Abductors"); table.addCell("" + abductorService.countAllAbductors()); document.add(table); if (currentUser != null) { // List ongoing cases document.setHeader(new HeaderFooter(new Phrase("List of ongoing cases as of " + date), false)); document.newPage(); float[] widths = { 0.05f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.05f, 0.15f, 0.1f, 0.1f, 0.05f }; PdfPTable pdfptable = new PdfPTable(widths); pdfptable.setWidthPercentage(100); pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Nickname", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Middle Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Case Type", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Photo", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Relative", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Abductor", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Investigator", FontFactory.getFont(FontFactory.HELVETICA, 12))); List<Person> personList = personService.listOngoing(); if (personList != null) { for (Person person : personList) { // Process the photo String tokens[] = person.getPhoto().split("\\/"); String defaultPhotoBasename = ""; for (int i = 0; i < tokens.length - 1; i++) { defaultPhotoBasename += tokens[i] + File.separator; } defaultPhotoBasename += tokens[tokens.length - 1]; String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/") + defaultPhotoBasename; Image image = Image.getInstance(absoluteDefaultPhotoFilename); image.scaleAbsolute(50, 75); image.setAlignment(Image.ALIGN_CENTER); pdfptable.addCell( new Phrase("" + person.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getNickname(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getMiddleName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(new Phrase(getResources(request).getMessage("type." + person.getType()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(getResources(request).getMessage("status.case." + person.getStatus()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(image); String relativeName = ""; if (person.getRelativeId() != null) { Relative relative = relativeService.getRelativeById(person.getRelativeId()); relativeName = relative.getFirstName() + " " + relative.getLastName(); } pdfptable.addCell(new Phrase(relativeName, FontFactory.getFont(FontFactory.HELVETICA, 8))); String abductorName = ""; if (person.getAbductorId() != null) { Abductor abductor = abductorService.getAbductorById(person.getAbductorId()); abductorName = abductor.getFirstName() + " " + abductor.getLastName(); } pdfptable.addCell(new Phrase(abductorName, FontFactory.getFont(FontFactory.HELVETICA, 8))); String investigatorUsername = ""; if (person.getInvestigatorId() != null) { User investigator = userService.getUserById(person.getInvestigatorId()); investigatorUsername = investigator.getUsername(); } pdfptable.addCell( new Phrase(investigatorUsername, FontFactory.getFont(FontFactory.HELVETICA, 8))); } } document.add(pdfptable); // List solved cases document.setHeader(new HeaderFooter(new Phrase("List of solved cases as of " + date), false)); document.newPage(); pdfptable = new PdfPTable(widths); pdfptable.setWidthPercentage(100); pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Nickname", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Middle Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Case Type", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Photo", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Relative", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Abductor", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Investigator", FontFactory.getFont(FontFactory.HELVETICA, 12))); personList = personService.listSolved(); if (personList != null) { for (Person person : personList) { // Process the photo String tokens[] = person.getPhoto().split("\\/"); String defaultPhotoBasename = ""; for (int i = 0; i < tokens.length - 1; i++) { defaultPhotoBasename += tokens[i] + File.separator; } defaultPhotoBasename += tokens[tokens.length - 1]; String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/") + defaultPhotoBasename; Image image = Image.getInstance(absoluteDefaultPhotoFilename); image.scaleAbsolute(50, 75); image.setAlignment(Image.ALIGN_CENTER); pdfptable.addCell( new Phrase("" + person.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getNickname(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getMiddleName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(new Phrase(getResources(request).getMessage("type." + person.getType()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(getResources(request).getMessage("status.case." + person.getStatus()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(image); String relativeName = ""; if (person.getRelativeId() != null) { Relative relative = relativeService.getRelativeById(person.getRelativeId()); relativeName = relative.getFirstName() + " " + relative.getLastName(); } pdfptable.addCell(new Phrase(relativeName, FontFactory.getFont(FontFactory.HELVETICA, 8))); String abductorName = ""; if (person.getAbductorId() != null) { Abductor abductor = abductorService.getAbductorById(person.getAbductorId()); abductorName = abductor.getFirstName() + " " + abductor.getLastName(); } pdfptable.addCell(new Phrase(abductorName, FontFactory.getFont(FontFactory.HELVETICA, 8))); String investigatorUsername = ""; if (person.getInvestigatorId() != null) { User investigator = userService.getUserById(person.getInvestigatorId()); investigatorUsername = investigator.getUsername(); } pdfptable.addCell( new Phrase(investigatorUsername, FontFactory.getFont(FontFactory.HELVETICA, 8))); } } document.add(pdfptable); // List unsolved cases document.setHeader(new HeaderFooter(new Phrase("List of unsolved cases as of " + date), false)); document.newPage(); pdfptable = new PdfPTable(widths); pdfptable.setWidthPercentage(100); pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Nickname", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Middle Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Case Type", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Photo", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Relative", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Abductor", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Investigator", FontFactory.getFont(FontFactory.HELVETICA, 12))); personList = personService.listUnsolved(); if (personList != null) { for (Person person : personList) { // Process the photo String tokens[] = person.getPhoto().split("\\/"); String defaultPhotoBasename = ""; for (int i = 0; i < tokens.length - 1; i++) { defaultPhotoBasename += tokens[i] + File.separator; } defaultPhotoBasename += tokens[tokens.length - 1]; String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/") + defaultPhotoBasename; Image image = Image.getInstance(absoluteDefaultPhotoFilename); image.scaleAbsolute(50, 75); image.setAlignment(Image.ALIGN_CENTER); pdfptable.addCell( new Phrase("" + person.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getNickname(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(person.getMiddleName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(new Phrase(getResources(request).getMessage("type." + person.getType()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(getResources(request).getMessage("status.case." + person.getStatus()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(image); String relativeName = ""; if (person.getRelativeId() != null) { Relative relative = relativeService.getRelativeById(person.getRelativeId()); relativeName = relative.getFirstName() + " " + relative.getLastName(); } pdfptable.addCell(new Phrase(relativeName, FontFactory.getFont(FontFactory.HELVETICA, 8))); String abductorName = ""; if (person.getAbductorId() != null) { Abductor abductor = abductorService.getAbductorById(person.getAbductorId()); abductorName = abductor.getFirstName() + " " + abductor.getLastName(); } pdfptable.addCell(new Phrase(abductorName, FontFactory.getFont(FontFactory.HELVETICA, 8))); String investigatorUsername = ""; if (person.getInvestigatorId() != null) { User investigator = userService.getUserById(person.getInvestigatorId()); investigatorUsername = investigator.getUsername(); } pdfptable.addCell( new Phrase(investigatorUsername, FontFactory.getFont(FontFactory.HELVETICA, 8))); } } document.add(pdfptable); } document.close(); // Set the response to return the poster (PDF file) response.setContentType("application/pdf"); response.setContentLength(baos.size()); response.setHeader("Content-disposition", "attachment; filename=Case_Statistics.pdf"); // Close the output stream baos.writeTo(response.getOutputStream()); response.getOutputStream().flush(); return null; }
From source file:com.googlecode.openmpis.action.PersonAction.java
License:Open Source License
/** * Prints the person's poster in PDF file. * * @param mapping the ActionMapping used to select this instance * @param form the optional ActionForm bean for this request * @param request the HTTP Request we are processing * @param response the HTTP Response we are processing * @return the forwarding instance * @throws java.lang.Exception/* w ww. j av a 2 s. c o m*/ */ public ActionForward printPoster(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Set the paper size and margins Document document = new Document(PageSize.LETTER, 50, 50, 50, 50); // Create the PDF writer ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); // Retrieve the person try { int id = Integer.parseInt(request.getParameter("id")); Person person = (Person) personService.getPersonById(id); // Process the photo String tokens[] = person.getPhoto().split("\\/"); String defaultPhotoBasename = ""; for (int i = 0; i < tokens.length - 1; i++) { defaultPhotoBasename += tokens[i] + File.separator; } defaultPhotoBasename += tokens[tokens.length - 1]; String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/") + defaultPhotoBasename; // Add some meta information to the document document.addTitle("Poster"); document.addAuthor("OpenMPIS"); document.addSubject("Poster for " + person.getNickname()); document.addKeywords("OpenMPIS, missing, found, unidentified"); document.addProducer(); document.addCreationDate(); document.addCreator("OpenMPIS version " + Constants.VERSION); // Open the document for writing document.open(); // Add the banner if (person.getType() > 4) { Paragraph foundParagraph = new Paragraph("F O U N D", FontFactory.getFont(FontFactory.HELVETICA_BOLD, 36, Font.BOLD, new Color(255, 0, 0))); foundParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(foundParagraph); } else { Paragraph missingParagraph = new Paragraph("M I S S I N G", FontFactory.getFont(FontFactory.HELVETICA_BOLD, 36, Font.BOLD, new Color(255, 0, 0))); missingParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(missingParagraph); } // Add date missing or found Paragraph blackParagraph = new Paragraph( getResources(request).getMessage("month." + person.getMonthMissingOrFound()) + " " + person.getDayMissingOrFound() + ", " + person.getYearMissingOrFound(), FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add missing from location if (person.getType() < 5) { blackParagraph = new Paragraph(person.getMissingFromCity() + ", " + person.getMissingFromProvince(), FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); } // Add name Paragraph redParagraph; if (!person.getNickname().isEmpty()) { redParagraph = new Paragraph( person.getFirstName() + " \"" + person.getNickname() + "\" " + person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); } else { redParagraph = new Paragraph(person.getFirstName() + " " + person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); } redParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(redParagraph); // Add the photo Image image = Image.getInstance(absoluteDefaultPhotoFilename); image.scaleAbsolute(200, 300); image.setAlignment(Image.ALIGN_CENTER); document.add(image); // Add description redParagraph = new Paragraph("Description", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); redParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(redParagraph); float[] widths = { 0.5f, 0.5f }; PdfPTable pdfptable = new PdfPTable(widths); pdfptable.setWidthPercentage(100); if (person.getType() < 5) { pdfptable .addCell( new Phrase( getResources(request).getMessage("label.date.birth") + ": " + getResources(request) .getMessage("month." + person.getBirthMonth()) + " " + person.getBirthDay() + ", " + person.getBirthYear(), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell( new Phrase(getResources(request).getMessage("label.address.city") + ": " + person.getCity(), FontFactory.getFont(FontFactory.HELVETICA, 12))); } pdfptable.addCell(new Phrase( getResources(request).getMessage("label.sex") + ": " + getResources(request).getMessage("sex." + person.getSex()), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable .addCell( new Phrase( getResources(request).getMessage("label.color.hair") + ": " + getResources(request) .getMessage("color.hair." + person.getHairColor()), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.height") + ": " + person.getFeet() + "' " + person.getInches() + "\"", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase( getResources(request).getMessage("label.color.eye") + ": " + getResources(request).getMessage("color.eye." + person.getEyeColor()), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase( getResources(request).getMessage("label.weight") + ": " + person.getWeight() + " " + getResources(request).getMessage("label.weight.lbs"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase( getResources(request).getMessage("label.race") + ": " + getResources(request).getMessage("race." + person.getRace()), FontFactory.getFont(FontFactory.HELVETICA, 12))); document.add(pdfptable); // Add circumstance redParagraph = new Paragraph(getResources(request).getMessage("label.circumstance"), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); redParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(redParagraph); blackParagraph = new Paragraph(person.getCircumstance(), FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL)); blackParagraph.setAlignment(Paragraph.ALIGN_JUSTIFIED); document.add(blackParagraph); // Add line blackParagraph = new Paragraph( "------------------------------------------------------------------------------"); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add contact blackParagraph = new Paragraph(getResources(request).getMessage("global.contact"), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL)); blackParagraph.setAlignment(Paragraph.ALIGN_JUSTIFIED); document.add(blackParagraph); document.close(); // Set the response to return the poster (PDF file) response.setContentType("application/pdf"); response.setContentLength(baos.size()); response.setHeader("Content-disposition", "attachment; filename=Poster.pdf"); // Close the output stream baos.writeTo(response.getOutputStream()); response.getOutputStream().flush(); return null; } catch (NumberFormatException nfe) { return mapping.findForward(Constants.LIST_PERSON); } catch (NullPointerException npe) { return mapping.findForward(Constants.LIST_PERSON); } }
From source file:com.googlecode.openmpis.action.ReportAction.java
License:Open Source License
/** * Prints the reports.//ww w. j a v a 2s. c o m * This is the print report action called from the Struts framework. * * @param mapping the ActionMapping used to select this instance * @param form the optional ActionForm bean for this request * @param request the HTTP Request we are processing * @param response the HTTP Response we are processing * @return the forwarding instance * @throws java.lang.Exception */ public ActionForward printReport(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { User currentUser = null; // Check if there exists a session if (request.getSession().getAttribute("currentuser") == null) { return mapping.findForward(Constants.EXPIRED); } else { currentUser = (User) request.getSession().getAttribute("currentuser"); } // Check if current user is authorized if ((currentUser.getGroupId() == 1) || (currentUser.getGroupId() == 2)) { // Retrieve person ID try { int personId = Integer.parseInt(request.getParameter("personid")); Person person = personService.getPersonById(personId); // Process the photo String tokens[] = person.getPhoto().split("\\/"); String defaultPhotoBasename = ""; for (int i = 0; i < tokens.length - 1; i++) { defaultPhotoBasename += tokens[i] + File.separator; } defaultPhotoBasename += tokens[tokens.length - 1]; String absoluteDefaultPhotoFilename = getServlet().getServletContext().getRealPath("/") + defaultPhotoBasename; // Set the paper size and margins Document document = new Document(PageSize.LETTER, 50, 50, 50, 50); // Create the PDF writer ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); // Add some meta information to the document document.addTitle("Progress Report"); document.addAuthor("OpenMPIS"); document.addSubject("Progress Report"); document.addKeywords("OpenMPIS, missing, found, unidentified"); document.addProducer(); document.addCreationDate(); document.addCreator("OpenMPIS version " + Constants.VERSION); // Set the header String date = simpleDateFormat.format(System.currentTimeMillis()); document.setHeader(new HeaderFooter(new Phrase("Report for " + person.getFirstName() + " \"" + person.getNickname() + "\" " + person.getLastName() + " as of " + date), false)); // Set the footer HeaderFooter footer = new HeaderFooter(new Phrase("Page : "), true); footer.setAlignment(Element.ALIGN_CENTER); document.setFooter(footer); // Open the document for writing document.open(); // Print the information on person // Add the banner if (person.getType() > 4) { Paragraph foundParagraph = new Paragraph("F O U N D", FontFactory.getFont(FontFactory.HELVETICA_BOLD, 36, Font.BOLD, new Color(255, 0, 0))); foundParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(foundParagraph); } else { Paragraph missingParagraph = new Paragraph("M I S S I N G", FontFactory.getFont(FontFactory.HELVETICA_BOLD, 36, Font.BOLD, new Color(255, 0, 0))); missingParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(missingParagraph); } // Add date missing or found Paragraph blackParagraph = new Paragraph( getResources(request).getMessage("month." + person.getMonthMissingOrFound()) + " " + person.getDayMissingOrFound() + ", " + person.getYearMissingOrFound(), FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); // Add missing from location if (person.getType() < 5) { blackParagraph = new Paragraph( person.getMissingFromCity() + ", " + person.getMissingFromProvince(), FontFactory.getFont(FontFactory.HELVETICA, 10, Font.BOLD, new Color(0, 0, 0))); blackParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(blackParagraph); } // Add name Paragraph redParagraph; if (!person.getNickname().isEmpty()) { redParagraph = new Paragraph( person.getFirstName() + " \"" + person.getNickname() + "\" " + person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); } else { redParagraph = new Paragraph(person.getFirstName() + " " + person.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); } redParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(redParagraph); // Add the photo Image image = Image.getInstance(absoluteDefaultPhotoFilename); image.scaleAbsolute(200, 300); image.setAlignment(Image.ALIGN_CENTER); document.add(image); // Add description redParagraph = new Paragraph("Description", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD, new Color(255, 0, 0))); redParagraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(redParagraph); float[] widths = { 0.5f, 0.5f }; PdfPTable pdfptable = new PdfPTable(widths); pdfptable.setWidthPercentage(100); if (person.getType() < 5) { pdfptable .addCell( new Phrase( getResources(request).getMessage("label.date.birth") + ": " + getResources(request) .getMessage("month." + person.getBirthMonth()) + " " + person.getBirthDay() + ", " + person.getBirthYear(), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase( getResources(request).getMessage("label.address.city") + ": " + person.getCity(), FontFactory.getFont(FontFactory.HELVETICA, 12))); } pdfptable.addCell(new Phrase( getResources(request).getMessage("label.sex") + ": " + getResources(request).getMessage("sex." + person.getSex()), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable .addCell( new Phrase( getResources(request).getMessage("label.color.hair") + ": " + getResources(request) .getMessage("color.hair." + person.getHairColor()), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable .addCell(new Phrase( getResources(request).getMessage("label.height") + ": " + person.getFeet() + "' " + person.getInches() + "\"", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable .addCell( new Phrase( getResources(request).getMessage("label.color.eye") + ": " + getResources(request) .getMessage("color.eye." + person.getEyeColor()), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase( getResources(request).getMessage("label.weight") + ": " + person.getWeight() + " " + getResources(request).getMessage("label.weight.lbs"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase( getResources(request).getMessage("label.race") + ": " + getResources(request).getMessage("race." + person.getRace()), FontFactory.getFont(FontFactory.HELVETICA, 12))); document.add(pdfptable); // Print information on relative Relative relative = relativeService.getRelativeById(person.getRelativeId()); document.newPage(); document.add(new Paragraph(getResources(request).getMessage("label.relative.name") + ": " + relative.getFirstName() + " " + relative.getLastName())); document.add(new Paragraph(getResources(request).getMessage("label.relation") + ": " + getResources(request).getMessage("relation." + person.getRelationToRelative()))); document.add(new Paragraph(getResources(request).getMessage("label.address") + ": " + relative.getStreet() + ", " + relative.getCity() + ", " + relative.getProvince())); document.add(new Paragraph( getResources(request).getMessage("label.number") + ": " + relative.getNumber())); document.add(new Paragraph( getResources(request).getMessage("label.email") + ": " + relative.getEmail())); // Print information on abductor if (person.getAbductorId() != null) { Abductor abductor = abductorService.getAbductorById(person.getAbductorId()); document.newPage(); document.add(new Paragraph(getResources(request).getMessage("label.abductor.name") + ": " + abductor.getFirstName() + " " + abductor.getLastName())); } // Print sightings if ((currentUser.getGroupId() == 1) || (currentUser.getGroupId() == 2)) { document.setHeader(new HeaderFooter(new Phrase("List of sightings as of " + date), false)); document.newPage(); float sightingsWidths[] = { 0.1f, 0.1f, 0.1f, 0.3f, 0.1f, 0.1f, 0.1f, 0.1f }; pdfptable = new PdfPTable(sightingsWidths); pdfptable.setWidthPercentage(100); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.id"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.date.sent"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.subject"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.message"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.lastname"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.firstname"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.email"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.ipaddress"), FontFactory.getFont(FontFactory.HELVETICA, 12))); List<Message> sightingList = messageService.listAllSightingsForPerson(personId); for (Message sighting : sightingList) { pdfptable.addCell( new Phrase("" + sighting.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(sighting.getDate(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(sighting.getSubject(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(sighting.getMessage(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(sighting.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(sighting.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(sighting.getEmail(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(sighting.getIpAddress(), FontFactory.getFont(FontFactory.HELVETICA, 8))); } document.add(pdfptable); } // Print progress reports if ((currentUser.getGroupId() == 1) || (currentUser.getGroupId() == 2)) { document.setHeader( new HeaderFooter(new Phrase("List of progress reports as of " + date), false)); document.newPage(); float reportsWidths[] = { 0.1f, 0.1f, 0.3f }; pdfptable = new PdfPTable(reportsWidths); pdfptable.setWidthPercentage(100); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.id"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.date.reported"), FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase(getResources(request).getMessage("label.report"), FontFactory.getFont(FontFactory.HELVETICA, 12))); List<Report> reportList = reportService.listAllReportsForPerson(personId); for (Report report : reportList) { pdfptable.addCell( new Phrase("" + report.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(report.getDate(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(report.getReport(), FontFactory.getFont(FontFactory.HELVETICA, 8))); } document.add(pdfptable); } document.close(); // Set the response to return the poster (PDF file) response.setContentType("application/pdf"); response.setContentLength(baos.size()); response.setHeader("Content-disposition", "attachment; filename=Report.pdf"); // Close the output stream baos.writeTo(response.getOutputStream()); response.getOutputStream().flush(); return null; } catch (NumberFormatException nfe) { return mapping.findForward(Constants.LIST_PERSON); } catch (NullPointerException npe) { return mapping.findForward(Constants.LIST_PERSON); } } else { return mapping.findForward(Constants.UNAUTHORIZED); } }
From source file:com.googlecode.openmpis.action.UserAction.java
License:Open Source License
/** * Writes the users to a PDF file./*from w w w . j a v a 2s. c om*/ * * @param mapping the ActionMapping used to select this instance * @param form the optional ActionForm bean for this request * @param request the HTTP Request we are processing * @param response the HTTP Response we are processing * @return the forwarding instance * @throws java.lang.Exception */ public ActionForward printUsers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { User currentUser = null; // Check if there exists a session if (request.getSession().getAttribute("currentuser") != null) { currentUser = (User) request.getSession().getAttribute("currentuser"); } // Set the paper size and margins Document document = new Document(PageSize.LETTER.rotate(), 50, 50, 50, 50); // Create the PDF writer ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); // Add some meta information to the document document.addTitle("Case Statistics"); document.addAuthor("OpenMPIS"); document.addSubject("Statistics for All Cases"); document.addKeywords("OpenMPIS, missing, found, unidentified"); document.addProducer(); document.addCreationDate(); document.addCreator("OpenMPIS version " + Constants.VERSION); // Set the header String date = simpleDateFormat.format(System.currentTimeMillis()); document.setHeader(new HeaderFooter(new Phrase("List of users as of " + date), false)); // Set the footer HeaderFooter footer = new HeaderFooter(new Phrase("Page : "), true); footer.setAlignment(Element.ALIGN_CENTER); document.setFooter(footer); // Open the document for writing document.open(); Table table = new Table(2); table.setBorderWidth(1); table.setPadding(2); table.setSpacing(0); Paragraph paragraph = new Paragraph("Users", FontFactory.getFont(FontFactory.HELVETICA, 24, Font.BOLD)); paragraph.setAlignment(Paragraph.ALIGN_CENTER); Cell cell = new Cell(paragraph); cell.setHeader(true); cell.setColspan(2); table.addCell(cell); table.endHeaders(); table.addCell("Total Administrators"); table.addCell("" + userService.countAdministrators()); table.addCell("\t\t\t\t\tActive Administrators"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + userService.countActiveAdministrators()); table.addCell("\t\t\t\t\tSuspended Administrators"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + userService.countSuspendedAdministrators()); table.addCell("Total Encoders"); table.addCell("" + userService.countEncoders()); table.addCell("\t\t\t\t\tActive Encoders"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + userService.countActiveEncoders()); table.addCell("\t\t\t\t\tSuspended Encoders"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + userService.countSuspendedEncoders()); table.addCell("Total Investigators"); table.addCell("" + userService.countInvestigators()); table.addCell("\t\t\t\t\tActive Investigators"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + userService.countActiveInvestigators()); table.addCell("\t\t\t\t\tSuspended Investigators"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + userService.countSuspendedInvestigators()); table.addCell("Total Users"); table.addCell("" + userService.countAllUsers()); table.addCell("\t\t\t\t\tTotal Active Users"); table.addCell("\t\t\t\t\t\t\t\t\t\t" + userService.countActiveUsers()); table.addCell("\t\t\t\t\tTotal Suspended Users"); table.addCell("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" + userService.countSuspendedUsers()); document.add(table); if (currentUser != null) { if ((currentUser.getGroupId() == 0) || (currentUser.getGroupId() == 1)) { // List administrators document.setHeader(new HeaderFooter(new Phrase("List of administrators as of " + date), false)); document.newPage(); float[] widths = { 0.03f, 0.07f, 0.1f, 0.1f, 0.1f, 0.1f, 0.2f, 0.05f }; PdfPTable pdfptable = new PdfPTable(widths); pdfptable.setWidthPercentage(100); pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Group", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Agency", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Designation", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("E-mail Address", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12))); List<User> administratorList = userService.listAdministrators(); for (User administrator : administratorList) { pdfptable.addCell( new Phrase("" + administrator.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(getResources(request).getMessage("group." + administrator.getGroupId()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(administrator.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(new Phrase(administrator.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(administrator.getAgency(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(new Phrase(administrator.getDesignation(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(administrator.getEmail(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(getResources(request).getMessage("status.user." + administrator.getStatus()), FontFactory.getFont(FontFactory.HELVETICA, 8))); } document.add(pdfptable); // List encoders document.setHeader(new HeaderFooter(new Phrase("List of encoders as of " + date), false)); document.newPage(); pdfptable = new PdfPTable(widths); pdfptable.setWidthPercentage(100); pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Group", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Agency", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Designation", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("E-mail Address", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12))); List<User> encoderList = userService.listEncoders(); for (User encoder : encoderList) { pdfptable.addCell( new Phrase("" + encoder.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(new Phrase(getResources(request).getMessage("group." + encoder.getGroupId()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(encoder.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(encoder.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(encoder.getAgency(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(encoder.getDesignation(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable .addCell(new Phrase(encoder.getEmail(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(getResources(request).getMessage("status.user." + encoder.getStatus()), FontFactory.getFont(FontFactory.HELVETICA, 8))); } document.add(pdfptable); // List investigators document.setHeader(new HeaderFooter(new Phrase("List of investigators as of " + date), false)); document.newPage(); pdfptable = new PdfPTable(widths); pdfptable.setWidthPercentage(100); pdfptable.addCell(new Phrase("ID", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Group", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Last Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("First Name", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Agency", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Designation", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("E-mail Address", FontFactory.getFont(FontFactory.HELVETICA, 12))); pdfptable.addCell(new Phrase("Status", FontFactory.getFont(FontFactory.HELVETICA, 12))); List<User> investigatorList = userService.listInvestigators(); for (User investigator : investigatorList) { pdfptable.addCell( new Phrase("" + investigator.getId(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(getResources(request).getMessage("group." + investigator.getGroupId()), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(investigator.getLastName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(investigator.getFirstName(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(investigator.getAgency(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell(new Phrase(investigator.getDesignation(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(investigator.getEmail(), FontFactory.getFont(FontFactory.HELVETICA, 8))); pdfptable.addCell( new Phrase(getResources(request).getMessage("status.user." + investigator.getStatus()), FontFactory.getFont(FontFactory.HELVETICA, 8))); } document.add(pdfptable); } } document.close(); // Set the response to return the poster (PDF file) response.setContentType("application/pdf"); response.setContentLength(baos.size()); response.setHeader("Content-disposition", "attachment; filename=User_Statistics.pdf"); // Close the output stream baos.writeTo(response.getOutputStream()); response.getOutputStream().flush(); return null; }
From source file:com.khs.report.writer.ReportPDFWriter.java
License:Apache License
private void addMetaData(Document document) { document.addTitle(metaTitle);/*from www. j a v a 2s. c o m*/ document.addSubject(metaSubject); document.addKeywords(metaKeywords); document.addAuthor(metaAuthor); document.addCreator(metaCreator); }
From source file:com.matic.sudoku.io.export.PdfExporter.java
License:Open Source License
/** * Generate and export multiple boards to PDF * /*from w ww. ja v a 2 s .c om*/ * @param exporterParameters PDF exporter parameters * @param generator Generator used for puzzle generation * @throws IOException If any PDF library error occurs * @throws DocumentException If any error occurs while writing the PDF file */ public void write(final ExporterParameters exporterParameters, final Generator generator, final int boardDimension) throws IOException, DocumentException { //How many PDF-pages are needed to fit all puzzles using the desired page formatting final int optimisticPageCount = exporterParameters.getPuzzleCount() / exporterParameters.getPuzzlesPerPage(); final int pageCount = exporterParameters.getPuzzleCount() % exporterParameters.getPuzzlesPerPage() > 0 ? optimisticPageCount + 1 : optimisticPageCount; final Document document = new Document(PageSize.A4, DOCUMENT_MARGIN, DOCUMENT_MARGIN, DOCUMENT_MARGIN, DOCUMENT_MARGIN); final OutputStream outputStream = new FileOutputStream(exporterParameters.getOutputPath()); final PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream); final String creator = Sudoku.getNameAndVersion(); document.addSubject("Puzzles generated by " + creator); document.addCreator(creator); document.open(); final Rectangle pageSize = document.getPageSize(); final int pageHeight = (int) pageSize.getHeight(); final int pageWidth = (int) pageSize.getWidth(); //Get appropriate number of rows and columns needed to divide a page into final int horizontalDimension = exporterParameters.getPuzzlesPerPage() > 2 ? 2 : 1; final int verticalDimension = exporterParameters.getPuzzlesPerPage() > 1 ? 2 : 1; //Get available space for each board (with margins) on a page final int boardWidth = pageWidth / horizontalDimension; final int boardHeight = pageHeight / verticalDimension; final Board board = new Board(boardDimension, SymbolType.DIGITS); board.setSize(boardWidth, boardHeight); board.handleResized(); //Get available height/width on a page for a puzzle itself final int puzzleWidth = board.getPuzzleWidth(); int puzzlesPrinted = 0; final PdfContentByte contentByte = pdfWriter.getDirectContent(); final Grading[] gradings = getGeneratedPuzzleGradings(exporterParameters.getGradings(), exporterParameters.getOrdering(), exporterParameters.getPuzzleCount()); pageCounter: for (int page = 0; page < pageCount; ++page) { document.newPage(); final Graphics2D g2d = contentByte.createGraphics(pageWidth, pageHeight); for (int y = 0, i = 0; i < verticalDimension; y += boardHeight, ++i) { for (int x = 0, j = 0; j < horizontalDimension; x += boardWidth, ++j) { //Check whether to generate a new puzzle or print empty boards final ExportMode exportMode = exporterParameters.getExportMode(); final Grading selectedGrading = exportMode == ExportMode.BLANK ? null : gradings[puzzlesPrinted]; if (exportMode == ExportMode.GENERATE_NEW) { board.setPuzzle(generatePuzzle(generator, getSymmetry(exporterParameters.getSymmetries()), selectedGrading)); board.recordGivens(); } //Calculate puzzle drawing origins final int originX = x + (int) (boardWidth / 2 - (puzzleWidth / 2)); final int originY = y + (int) (boardHeight / 2) - (puzzleWidth / 2); board.setSymbolType(getSymbolType(exporterParameters.getSymbolType())); board.setDrawingOrigin(originX, originY); board.draw(g2d, false, false); drawLegend(g2d, getLegendString(exporterParameters.isShowNumbering(), exporterParameters.isShowGrading(), puzzlesPrinted + 1, selectedGrading), originX, originY, puzzleWidth); if (++puzzlesPrinted == exporterParameters.getPuzzleCount()) { //We've printed all puzzles, break g2d.dispose(); break pageCounter; } } } g2d.dispose(); } document.close(); outputStream.flush(); outputStream.close(); }
From source file:com.qcadoo.report.internal.PdfHelperImpl.java
License:Open Source License
@Override public void addMetaData(final Document document) { document.addSubject("Using iText"); document.addKeywords("Java, PDF, iText"); document.addAuthor("QCADOO"); document.addCreator("QCADOO"); }
From source file:com.stratelia.webactiv.almanach.control.AlmanachPdfGenerator.java
License:Open Source License
static public void buildPdf(String name, AlmanachSessionController almanach, String mode) throws AlmanachRuntimeException { try {// www. ja v a 2 s . c o m SilverTrace.info("almanach", "AlmanachPdfGenerator.buildPdf()", "root.MSG_GEN_ENTER_METHOD"); String fileName = FileRepositoryManager.getTemporaryPath(almanach.getSpaceId(), almanach.getComponentId()) + name; Document document = new Document(PageSize.A4, 50, 50, 50, 50); // we add some meta information to the document document.addAuthor(almanach.getSettings().getString("author", "")); document.addSubject(almanach.getSettings().getString("subject", "")); document.addCreationDate(); PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); try { Calendar currentDay = Calendar.getInstance(); currentDay.setTime(almanach.getCurrentDay()); String sHeader = almanach.getString("events"); if (mode.equals(PDF_MONTH_ALLDAYS) || mode.equals(PDF_MONTH_EVENTSONLY)) { sHeader += " " + almanach.getString("GML.mois" + currentDay.get(Calendar.MONTH)); } sHeader += " " + currentDay.get(Calendar.YEAR); HeaderFooter header = new HeaderFooter(new Phrase(sHeader), false); HeaderFooter footer = new HeaderFooter(new Phrase("Page "), true); footer.setAlignment(Element.ALIGN_CENTER); document.setHeader(header); document.setFooter(footer); createFirstPage(almanach, document); document.newPage(); Font titleFont = new Font(Font.HELVETICA, 24, Font.NORMAL, new Color(255, 255, 255)); Paragraph cTitle = new Paragraph(almanach.getString("Almanach") + " " + almanach.getString("GML.mois" + currentDay.get(Calendar.MONTH)) + " " + currentDay.get(Calendar.YEAR), titleFont); Chapter chapter = new Chapter(cTitle, 1); // Collection<EventDetail> events = // almanach.getListRecurrentEvent(mode.equals(PDF_YEAR_EVENTSONLY)); AlmanachCalendarView almanachView; if (PDF_YEAR_EVENTSONLY.equals(mode)) { almanachView = almanach.getYearlyAlmanachCalendarView(); } else { almanachView = almanach.getMonthlyAlmanachCalendarView(); } List<DisplayableEventOccurrence> occurrences = almanachView.getEvents(); generateAlmanach(chapter, almanach, occurrences, mode); document.add(chapter); } catch (Exception ex) { throw new AlmanachRuntimeException("PdfGenerator.generate", AlmanachRuntimeException.WARNING, "AlmanachRuntimeException.EX_PROBLEM_TO_GENERATE_PDF", ex); } document.close(); SilverTrace.info("almanach", "AlmanachPdfGenerator.buildPdf()", "root.MSG_GEN_EXIT_METHOD"); } catch (Exception e) { throw new AlmanachRuntimeException("PdfGenerator.generate", AlmanachRuntimeException.WARNING, "AlmanachRuntimeException.EX_PROBLEM_TO_GENERATE_PDF", e); } }