List of usage examples for com.lowagie.text Paragraph Paragraph
public Paragraph(float leading, String string)
Paragraph
with a certain String
and a certain leading. From source file:com.crm.webapp.util.PDFCustomExporter.java
License:Apache License
protected void exportRow(DataTable table, PdfPTable pdfTable, int rowIndex) { table.setRowIndex(rowIndex);/* ww w. j a va 2 s .c o m*/ if (!table.isRowAvailable()) { return; } exportCells(table, pdfTable, rowIndex); SummaryRow sr = table.getSummaryRow(); if (sr != null && sr.isInView()) { for (UIComponent summaryComponent : sr.getChildren()) { UIColumn column = (UIColumn) summaryComponent; StringBuilder builder = new StringBuilder(); for (UIComponent component : column.getChildren()) { if (component.isRendered()) { String value = exportValue(FacesContext.getCurrentInstance(), component); if (value != null) { builder.append(value); } } } int rowSpan = column.getRowspan(); int colSpan = column.getColspan(); PdfPCell cell = new PdfPCell(new Paragraph(builder.toString(), this.facetFont)); if (facetBackground != null) { cell.setBackgroundColor(facetBackground); } if (rowSpan > 1) { cell.setVerticalAlignment(Element.ALIGN_CENTER); cell.setRowspan(rowSpan); } if (colSpan > 1) { cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setColspan(colSpan); } pdfTable.addCell(cell); } } }
From source file:com.crm.webapp.util.PDFCustomExporter.java
License:Apache License
protected void addColumnFacets(DataTable table, PdfPTable pdfTable, ColumnType columnType) { for (UIColumn col : table.getColumns()) { if (!col.isRendered()) { continue; }/* w w w . j ava 2s . co m*/ if (col instanceof DynamicColumn) { ((DynamicColumn) col).applyModel(); } PdfPCell cell = null; if (col.isExportable()) { if (col.getHeaderText() != null && columnType.name().equalsIgnoreCase("header")) { cell = new PdfPCell(new Paragraph(col.getHeaderText(), this.facetFont)); if (facetBackground != null) { cell.setBackgroundColor(facetBackground); } cell.setHorizontalAlignment(Element.ALIGN_CENTER); pdfTable.addCell(cell); } else if (col.getFooterText() != null && columnType.name().equalsIgnoreCase("footer")) { cell = new PdfPCell(new Paragraph(col.getFooterText(), this.facetFont)); if (facetBackground != null) { cell.setBackgroundColor(facetBackground); } pdfTable.addCell(cell); } else { UIComponent component = (UIComponent) col; //Adding RowIndex for Custom Exporter if (component.getId().equalsIgnoreCase("subject")) { String value = "Index"; PdfPCell cellIndex = new PdfPCell(new Paragraph(value)); //addColumnAlignments(component, cell); if (facetBackground != null) { cellIndex.setBackgroundColor(facetBackground); } cellIndex.setHorizontalAlignment(Element.ALIGN_CENTER); pdfTable.addCell(cellIndex); } addColumnValue(pdfTable, col.getFacet(columnType.facet()), this.facetFont, columnType.name()); } } } }
From source file:com.crm.webapp.util.PDFCustomExporter.java
License:Apache License
protected void addColumnFacets(SubTable table, PdfPTable pdfTable, ColumnType columnType) { for (UIColumn col : table.getColumns()) { if (!col.isRendered()) { continue; }/*w w w . j a va 2s . c o m*/ if (col instanceof DynamicColumn) { ((DynamicColumn) col).applyModel(); } PdfPCell cell = null; if (col.isExportable()) { if (col.getHeaderText() != null && columnType.name().equalsIgnoreCase("header")) { cell = new PdfPCell(new Paragraph(col.getHeaderText(), this.facetFont)); if (facetBackground != null) { cell.setBackgroundColor(facetBackground); } cell.setHorizontalAlignment(Element.ALIGN_CENTER); pdfTable.addCell(cell); } else if (col.getFooterText() != null && columnType.name().equalsIgnoreCase("footer")) { cell = new PdfPCell(new Paragraph(col.getFooterText(), this.facetFont)); if (facetBackground != null) { cell.setBackgroundColor(facetBackground); } pdfTable.addCell(cell); } else { addColumnValue(pdfTable, col.getFacet(columnType.facet()), this.facetFont, columnType.name()); } } } }
From source file:com.crm.webapp.util.PDFCustomExporter.java
License:Apache License
protected void addColumnValue(PdfPTable pdfTable, UIComponent component, Font font, String columnType) { String value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component); PdfPCell cell = new PdfPCell(new Paragraph(value, font)); //addColumnAlignments(component, cell); if (facetBackground != null) { cell.setBackgroundColor(facetBackground); }//from www. j ava 2s . c o m if (columnType.equalsIgnoreCase("header")) { cell.setHorizontalAlignment(Element.ALIGN_CENTER); } pdfTable.addCell(cell); }
From source file:com.crm.webapp.util.PDFCustomExporter.java
License:Apache License
protected void addColumnValue(PdfPTable pdfTable, List<UIComponent> components, Font font, String columnType) { StringBuilder builder = new StringBuilder(); for (UIComponent component : components) { if (component.isRendered()) { String value = exportValue(FacesContext.getCurrentInstance(), component); if (value != null) { builder.append(value);// w w w. j ava2s.co m } } } PdfPCell cell = new PdfPCell(new Paragraph(builder.toString(), font)); //addColumnAlignments(components, cell); if (columnType.equalsIgnoreCase("header")) { cell.setHorizontalAlignment(Element.ALIGN_CENTER); } pdfTable.addCell(cell); }
From source file:com.ev.export.AnnualPDFExporter.java
License:Apache License
private void addHeading() throws Exception { Element elem = new Paragraph("Energieverbrauch " + year, new Font(Font.TIMES_ROMAN, 24)); document.add(elem);/*from w ww . j av a 2 s . c o m*/ }
From source file:com.fcore.base.fileSystem.utils.FileUtil.java
/** * convert txt file to pdf /*www . ja v a2 s .c o m*/ * @param originalPath * @param dirPath */ public static void txt2pdf(String originalPath, String dirPath) { com.lowagie.text.Document document = null; BufferedReader read = null; long old = System.currentTimeMillis(); try { document = new com.lowagie.text.Document(PageSize.A4, 80, 80, 60, 30); PdfWriter.getInstance(document, new FileOutputStream(dirPath)); document.open(); BaseFont bfChinese = BaseFont.createFont(ReadCreatePdf.class.getResource("/") + "/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font FontChinese = new Font(bfChinese, 18, Font.NORMAL); FileInputStream fstream = new FileInputStream(originalPath); DataInputStream in = new DataInputStream(fstream); read = new BufferedReader(new InputStreamReader(in, "gb2312")); String line = null; while ((line = read.readLine()) != null) { Paragraph t = new Paragraph(line, FontChinese); t.setAlignment(Element.ALIGN_LEFT); t.setLeading(20.0f); document.add(t); } } catch (Exception e) { e.printStackTrace(); } finally { try { read.close(); document.close(); } catch (IOException e) { e.printStackTrace(); } } long now = System.currentTimeMillis(); System.out.println("" + ((now - old) / 1000.0) + "\n\n" + "?:" + dirPath); }
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 . java 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 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./* ww w . j a v a 2s . co m*/ * * @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 a va 2s . com */ 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); } }