List of usage examples for com.lowagie.text.pdf PdfPCell PdfPCell
public PdfPCell(PdfPCell cell)
PdfPCell
. From source file:fr.univlorraine.mondossierweb.controllers.NoteController.java
License:Apache License
/** * /*w w w. j a v a2 s. c o m*/ * @param document pdf */ public void creerPdfDetail(final Document document, Etudiant etudiant, Etape etape) { //configuration des fonts Font normal = FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, Font.NORMAL); Font normalbig = FontFactory.getFont(FontFactory.TIMES_ROMAN, 11, Font.BOLD); Font legerita = FontFactory.getFont(FontFactory.TIMES_ROMAN, 9, Font.ITALIC); Font headerbig = FontFactory.getFont(FontFactory.TIMES_ROMAN, 16, Font.BOLD); Font header = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD); //pieds de pages: Date d = new Date(); DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); String date = dateFormat.format(d); //alignement des libells du pied de page: String partie1 = applicationContext.getMessage("pdf.notes.detail", null, Locale.getDefault()); String partie2 = applicationContext.getMessage("pdf.edition.date", null, Locale.getDefault()) + " : " + date; if (partie1.length() < ECARTEMENT_PIED_PAGE_PDF) { int diff = ECARTEMENT_PIED_PAGE_PDF - partie1.length(); for (int i = 0; i < diff; i++) { partie1 = partie1 + " "; } } if (partie2.length() < ECARTEMENT_PIED_PAGE_PDF) { int diff = ECARTEMENT_PIED_PAGE_PDF - partie2.length(); for (int i = 0; i < diff; i++) { partie2 = " " + partie2; } } //creation du pied de page: Phrase phra = new Phrase( partie1 + " -" + applicationContext.getMessage("pdf.page", null, Locale.getDefault()), legerita); Phrase phra2 = new Phrase("- " + partie2, legerita); HeaderFooter hf = new HeaderFooter(phra, phra2); hf.setAlignment(HeaderFooter.ALIGN_CENTER); document.setFooter(hf); document.setFooter(hf); //ouverte du document. document.open(); try { //ajout image test if (configController.getLogoUniversitePdf() != null && !configController.getLogoUniversitePdf().equals("")) { Image image1 = Image.getInstance(configController.getLogoUniversitePdf()); float scaleRatio = 40 / image1.getHeight(); float newWidth = scaleRatio * image1.getWidth(); image1.scaleAbsolute(newWidth, 40); image1.setAbsolutePosition(800 - newWidth, 528); document.add(image1); } //nouveau paragraphe Paragraph p = new Paragraph(applicationContext.getMessage("pdf.notes.title", null, Locale.getDefault()) .toUpperCase(Locale.getDefault()) + "\n\n", headerbig); p.setIndentationLeft(15); document.add(p); if (etudiant.getNom() != null) { Paragraph p0 = new Paragraph(etudiant.getNom(), normal); p0.setIndentationLeft(15); document.add(p0); } if (etudiant.getCod_etu() != null) { Paragraph p01 = new Paragraph(applicationContext.getMessage("pdf.folder", null, Locale.getDefault()) + " : " + etudiant.getCod_etu(), normal); p01.setIndentationLeft(15); document.add(p01); } if (etudiant.getCod_nne() != null) { Paragraph p02 = new Paragraph(applicationContext.getMessage("pdf.nne", null, Locale.getDefault()) + " : " + etudiant.getCod_nne(), normal); p02.setIndentationLeft(15); document.add(p02); } if (etudiant.getEmail() != null) { Paragraph p03 = new Paragraph(applicationContext.getMessage("pdf.mail", null, Locale.getDefault()) + " : " + etudiant.getEmail(), normal); p03.setIndentationLeft(15); document.add(p03); } Paragraph p03 = new Paragraph( applicationContext.getMessage("pdf.edition.date", null, Locale.getDefault()) + " : " + date, normal); p03.setIndentationLeft(15); document.add(p03); document.add(new Paragraph("\n")); //Partie des notes PdfPTable table = new PdfPTable(1); table.setWidthPercentage(98); //PdfPCell cell = new PdfPCell(new Paragraph(applicationContext.getMessage("pdf.elements.epreuves", null, Locale.getDefault()).toUpperCase(Locale.getDefault()) + " - "+applicationContext.getMessage("pdf.annee.universitaire", null, Locale.getDefault()) + " : " + etape.getAnnee(), header)); PdfPCell cell = new PdfPCell(new Paragraph(etape.getLibelle() + " - " + applicationContext.getMessage("pdf.annee.universitaire", null, Locale.getDefault()) + " : " + etape.getAnnee(), header)); cell.setBorder(Rectangle.NO_BORDER); cell.setBackgroundColor(new Color(153, 153, 255)); table.addCell(cell); PdfPTable table2; boolean afficherRangElpEpr = etudiantController.isAfficherRangElpEpr(); boolean affRangEtudiant = configController.isAffRangEtudiant(); boolean affECTSEtudiant = configController.isAffECTSEtudiant(); if ((!affRangEtudiant && !afficherRangElpEpr) && !affECTSEtudiant) { //NI isAffRangEtudiant NI isAffECTSEtudiant table2 = new PdfPTable(6); table2.setWidthPercentage(98); int[] tabWidth = { 35, 110, 25, 25, 25, 25 }; table2.setWidths(tabWidth); } else { if (((affRangEtudiant || afficherRangElpEpr) && !affECTSEtudiant) || ((!affRangEtudiant && !afficherRangElpEpr) && affECTSEtudiant)) { //isAffRangEtudiant OU isAffECTSEtudiant table2 = new PdfPTable(7); table2.setWidthPercentage(98); int[] tabWidth = { 33, 110, 22, 22, 22, 22, 15 }; table2.setWidths(tabWidth); } else { //isAffRangEtudiant ET isAffECTSEtudiant table2 = new PdfPTable(8); table2.setWidthPercentage(98); int[] tabWidth = { 33, 110, 22, 22, 22, 22, 15, 15 }; table2.setWidths(tabWidth); } } //Paragraph p1 = new Paragraph(applicationContext.getMessage("pdf.year", null, Locale.getDefault()),normalbig); Paragraph p2 = new Paragraph(applicationContext.getMessage("pdf.code", null, Locale.getDefault()), normalbig); Paragraph p3 = new Paragraph(applicationContext.getMessage("pdf.label", null, Locale.getDefault()), normalbig); Paragraph parRang = new Paragraph(applicationContext.getMessage("pdf.rank", null, Locale.getDefault()), normalbig); Paragraph parEcts = new Paragraph(applicationContext.getMessage("pdf.ects", null, Locale.getDefault()), normalbig); PdfPCell ct4 = new PdfPCell(new Paragraph( applicationContext.getMessage("pdf.session", null, Locale.getDefault()) + " 1", normalbig)); PdfPCell ct5 = new PdfPCell(new Paragraph( applicationContext.getMessage("pdf.resultat", null, Locale.getDefault()), normalbig)); PdfPCell ct6 = new PdfPCell(new Paragraph( applicationContext.getMessage("pdf.session", null, Locale.getDefault()) + " 2", normalbig)); PdfPCell ct7 = new PdfPCell(new Paragraph( applicationContext.getMessage("pdf.resultat", null, Locale.getDefault()), normalbig)); //PdfPCell ct1 = new PdfPCell(p1); PdfPCell ct2 = new PdfPCell(p2); PdfPCell ct3 = new PdfPCell(p3); PdfPCell cellRang = new PdfPCell(parRang); PdfPCell cellEcts = new PdfPCell(parEcts); //ct1.setBorder(Rectangle.BOTTOM); ct1.setBorderColorBottom(Color.black); ct2.setBorder(Rectangle.BOTTOM); ct2.setBorderColorBottom(Color.black); ct3.setBorder(Rectangle.BOTTOM); ct3.setBorderColorBottom(Color.black); ct4.setBorder(Rectangle.BOTTOM); ct4.setBorderColorBottom(Color.black); ct5.setBorder(Rectangle.BOTTOM); ct5.setBorderColorBottom(Color.black); ct6.setBorder(Rectangle.BOTTOM); ct6.setBorderColorBottom(Color.black); ct7.setBorder(Rectangle.BOTTOM); ct7.setBorderColorBottom(Color.black); cellRang.setBorder(Rectangle.BOTTOM); cellRang.setBorderColorBottom(Color.black); cellEcts.setBorder(Rectangle.BOTTOM); cellEcts.setBorderColorBottom(Color.black); //table2.addCell(ct1); table2.addCell(ct2); table2.addCell(ct3); table2.addCell(ct4); table2.addCell(ct5); table2.addCell(ct6); table2.addCell(ct7); if ((affRangEtudiant || afficherRangElpEpr)) { table2.addCell(cellRang); } if (affRangEtudiant) { table2.addCell(cellEcts); } for (int i = 0; i < etudiant.getElementsPedagogiques().size(); i++) { /*String annee = etudiant.getElementsPedagogiques().get(i).getAnnee().replaceAll(applicationContext.getMessage("pdf.replace.ficm", null, Locale.getDefault()), ""); Paragraph pa = new Paragraph(annee, normal); PdfPCell celltext = new PdfPCell(pa); celltext.setBorder(Rectangle.NO_BORDER); table2.addCell(celltext);*/ Paragraph pa2 = new Paragraph(etudiant.getElementsPedagogiques().get(i).getCode(), normal); PdfPCell celltext2 = new PdfPCell(pa2); celltext2.setBorder(Rectangle.NO_BORDER); table2.addCell(celltext2); String indentation = ""; for (int j = 0; j < etudiant.getElementsPedagogiques().get(i).getLevel(); j++) { indentation = indentation + " "; } Paragraph pa3 = new Paragraph(indentation + etudiant.getElementsPedagogiques().get(i).getLibelle(), normal); PdfPCell celltext3 = new PdfPCell(pa3); celltext3.setBorder(Rectangle.NO_BORDER); table2.addCell(celltext3); Paragraph pa5 = new Paragraph(getNote1(etudiant.getElementsPedagogiques().get(i)), normal); PdfPCell celltext5 = new PdfPCell(pa5); celltext5.setBorder(Rectangle.NO_BORDER); table2.addCell(celltext5); Paragraph pa6 = new Paragraph(etudiant.getElementsPedagogiques().get(i).getRes1(), normal); PdfPCell celltext6 = new PdfPCell(pa6); celltext6.setBorder(Rectangle.NO_BORDER); table2.addCell(celltext6); Paragraph pa7 = new Paragraph(getNote2(etudiant.getElementsPedagogiques().get(i)), normal); PdfPCell celltext7 = new PdfPCell(pa7); celltext7.setBorder(Rectangle.NO_BORDER); table2.addCell(celltext7); Paragraph pa8 = new Paragraph(etudiant.getElementsPedagogiques().get(i).getRes2(), normal); PdfPCell celltext8 = new PdfPCell(pa8); celltext8.setBorder(Rectangle.NO_BORDER); table2.addCell(celltext8); if ((affRangEtudiant || afficherRangElpEpr)) { Paragraph parRang2 = new Paragraph(etudiant.getElementsPedagogiques().get(i).getRang(), normal); PdfPCell cellRang2 = new PdfPCell(parRang2); cellRang2.setBorder(Rectangle.NO_BORDER); table2.addCell(cellRang2); } if (affECTSEtudiant) { Paragraph parEcts2 = new Paragraph(etudiant.getElementsPedagogiques().get(i).getEcts(), normal); PdfPCell cellEcts2 = new PdfPCell(parEcts2); cellEcts2.setBorder(Rectangle.NO_BORDER); table2.addCell(cellEcts2); } } document.add(table); document.add(table2); document.add(new Paragraph("\n")); //Partie QUESTIONS if (etudiant.isSignificationResultatsUtilisee()) { PdfPTable tablequestions = new PdfPTable(1); tablequestions.setWidthPercentage(98); PdfPCell cellquestions = new PdfPCell( new Paragraph(applicationContext.getMessage("pdf.questions", null, Locale.getDefault()) .toUpperCase(Locale.getDefault()) + " ", header)); cellquestions.setBorder(Rectangle.NO_BORDER); cellquestions.setBackgroundColor(new Color(153, 153, 255)); tablequestions.addCell(cellquestions); PdfPTable tablequestions2 = new PdfPTable(1); tablequestions2.setWidthPercentage(98); String grilleSignficationResultats = ""; Set<String> ss = etudiant.getSignificationResultats().keySet(); for (String k : ss) { if (k != null && !k.equals("") && !k.equals(" ")) { grilleSignficationResultats = grilleSignficationResultats + k + " : " + etudiant.getSignificationResultats().get(k); grilleSignficationResultats = grilleSignficationResultats + " "; } } PdfPCell cellquestions2 = new PdfPCell(new Paragraph( applicationContext.getMessage("pdf.code.resultat.signification", null, Locale.getDefault()) + " : \n" + grilleSignficationResultats, normal)); cellquestions2.setBorder(Rectangle.NO_BORDER); tablequestions2.addCell(cellquestions2); document.add(tablequestions); document.add(tablequestions2); } } catch (BadElementException e) { LOG.error("Erreur la gnration du detail des notes : BadElementException ", e); } catch (MalformedURLException e) { LOG.error("Erreur la gnration du detail des notes : MalformedURLException ", e); } catch (IOException e) { LOG.error("Erreur la gnration du detail des notes : IOException ", e); } catch (DocumentException e) { LOG.error("Erreur la gnration du detail des notes : DocumentException ", e); } // step 6: fermeture du document. document.close(); }
From source file:geoportal.presentacion.beans.ReportesControlador.java
public void imprimirReporte() { //DateFormat dfDateFull = DateFormat.getDateInstance(DateFormat.FULL); try {//from w w w .j a v a 2 s . co m //Generamos el archivo PDF String directorioArchivos; ServletContext ctx = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext(); directorioArchivos = ctx.getRealPath("/") + "reportes"; String name = directorioArchivos + "/document-reporte.pdf"; Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(name)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(name)); //PdfWriter writer = PdfWriter.getInstance(document, //new FileOutputStream("C:")); Paragraph paragraph = new Paragraph(); Paragraph paragraph1 = new Paragraph(); Paragraph paragraph2 = new Paragraph(); //PdfPTable table = new PdfPTable(4); PdfPTable table1 = new PdfPTable(1); PdfPTable table2 = new PdfPTable(4); PdfPTable table3 = new PdfPTable(4); PdfPTable table5 = new PdfPTable(1); paragraph.add("\n\n\n\n\n\n\n"); paragraph.setAlignment(Paragraph.ALIGN_CENTER); paragraph1.add("\n"); paragraph1.setAlignment(Paragraph.ALIGN_CENTER); paragraph2.add("Total Denuncias:" + totalDenuncias); paragraph2.setAlignment(Paragraph.ALIGN_LEFT); // Obtenemos una instancia de nuestro manejador de eventos MembreteHeaderiText header = new MembreteHeaderiText(); //Asignamos el manejador de eventos al escritor. writer.setPageEvent(header); document.open(); // Chunk titulo = new Chunk(CHUNK, FontFactory.getFont(FontFactory.COURIER, 20, Font.UNDERLINE, BaseColor.BLACK)); // // titulo = new Chunk(IMAGE, FontFactory.getFont(FontFactory.COURIER, 20, Font.UNDERLINE, BaseColor.BLACK)); // document.add(titulo); // Image foto = Image.getInstance(resources / ferrari.jpg?); //foto.scaleToFit(100, 100); foto.setAlignment(Chunk.ALIGN_MIDDLE); //primera linea PdfPCell cell5 = new PdfPCell(new Paragraph("VIOLENCIA INTRAFAMILIAR ")); //segunda linea PdfPCell cell12 = new PdfPCell(new Paragraph("AO")); PdfPCell cell6 = new PdfPCell(new Paragraph("2010")); PdfPCell cell7 = new PdfPCell(new Paragraph("2011")); PdfPCell cell8 = new PdfPCell(new Paragraph("2012")); //tercera fila PdfPCell cell13 = new PdfPCell(new Paragraph("# DENUNCIAS")); PdfPCell cell9 = new PdfPCell(new Paragraph("" + lstVif2010.size())); PdfPCell cell10 = new PdfPCell(new Paragraph("" + lstVif2011.size())); PdfPCell cell11 = new PdfPCell(new Paragraph("" + lstVif_2012.size())); PdfPCell cell15 = new PdfPCell(new Paragraph("TOTAL DENUNCIAS:" + totalDenuncias)); cell5.setHorizontalAlignment(Element.ALIGN_CENTER); cell6.setHorizontalAlignment(Element.ALIGN_CENTER); cell7.setHorizontalAlignment(Element.ALIGN_CENTER); cell8.setHorizontalAlignment(Element.ALIGN_CENTER); cell9.setHorizontalAlignment(Element.ALIGN_CENTER); cell10.setHorizontalAlignment(Element.ALIGN_CENTER); cell11.setHorizontalAlignment(Element.ALIGN_CENTER); cell12.setHorizontalAlignment(Element.ALIGN_CENTER); cell13.setHorizontalAlignment(Element.ALIGN_CENTER); cell15.setHorizontalAlignment(Element.ALIGN_LEFT); cell12.setBackgroundColor(Color.cyan); cell13.setBackgroundColor(Color.cyan); cell5.setBorder(Rectangle.NO_BORDER); cell15.setBorder(Rectangle.NO_BORDER); table1.addCell(cell5); //aadir segunda fila table2.addCell(cell12); table2.addCell(cell6); table2.addCell(cell7); table2.addCell(cell8); //aadir tercera fila table3.addCell(cell13); table3.addCell(cell9); table3.addCell(cell10); table3.addCell(cell11); //aadir cuarta fila table5.addCell(cell15); document.add(paragraph); document.add(table1); document.add(paragraph1); document.add(table2); document.add(table3); document.add(table5); //document.add(paragraph2); //document.add(table); //document.setFooter(event); document.close(); //---------------------------- //Abrimos el archivo PDF FacesContext context = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); response.setContentType("application/pdf"); response.setHeader("Content-disposition", "inline=filename=" + name); try { response.getOutputStream().write(Util.getBytesFromFile(new File(name))); response.getOutputStream().flush(); response.getOutputStream().close(); context.responseComplete(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:gov.medicaid.services.impl.ExportServiceBean.java
License:Apache License
/** * Adds a centered cell to the given table. * //w w w . j a v a 2 s. c o m * @param table * the table to add the cell to * @param value * the value text */ private static void addCenterCell(PdfPTable table, String value) { PdfPCell val = new PdfPCell( new Phrase(Util.defaultString(value), FontFactory.getFont(FontFactory.HELVETICA, 7))); val.setBorder(Rectangle.NO_BORDER); val.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); table.addCell(val); }
From source file:gov.medicaid.services.impl.ExportServiceBean.java
License:Apache License
/** * Creates a header cell./*from w ww. j a va 2 s . c o m*/ * * @param text * the header text * @param colspan * the span of the header * @return the generated cell. */ private static PdfPCell createHeaderCell(String text, int colspan) { PdfPCell header = new PdfPCell(new Phrase(text, FontFactory.getFont(FontFactory.HELVETICA_BOLD))); header.setBorder(Rectangle.BOTTOM); header.setColspan(colspan); return header; }
From source file:gov.medicaid.services.util.PDFHelper.java
License:Apache License
/** * Creates a header cell./*from w ww .j a v a 2s .c o m*/ * * @param text the header text * @param colspan the span of the header * @return the generated cell. */ public static PdfPCell createHeaderCell(String text, int colspan) { PdfPCell header = new PdfPCell(new Phrase(text, FontFactory.getFont(FontFactory.HELVETICA_BOLD))); header.setBorder(Rectangle.BOTTOM); header.setColspan(colspan); return header; }
From source file:gov.medicaid.services.util.PDFHelper.java
License:Apache License
/** * Adds a label to the given table.//from www .j av a 2s . c om * * @param table the table to add to * @param label the label text */ public static void addLabel(PdfPTable table, String label) { PdfPCell key = new PdfPCell( new Phrase(Util.defaultString(label), FontFactory.getFont(FontFactory.HELVETICA_BOLD, 8))); key.setBorder(Rectangle.NO_BORDER); table.addCell(key); }
From source file:gov.medicaid.services.util.PDFHelper.java
License:Apache License
/** * Adds a cell to the given table.//w w w . jav a 2 s. co m * * @param table the table to add the cell to * @param value the value text */ public static void addCell(PdfPTable table, String value) { PdfPCell val = new PdfPCell( new Phrase(": " + Util.defaultString(value), FontFactory.getFont(FontFactory.HELVETICA, 7))); val.setBorder(Rectangle.NO_BORDER); val.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); table.addCell(val); }
From source file:gov.medicaid.services.util.PDFHelper.java
License:Apache License
/** * Adds a centered cell to the given table. * * @param table the table to add the cell to * @param value the value text//w ww .j a va2s. c o m */ public static void addCenterCell(PdfPTable table, String value) { PdfPCell val = new PdfPCell( new Phrase(Util.defaultString(value), FontFactory.getFont(FontFactory.HELVETICA, 7))); val.setBorder(Rectangle.NO_BORDER); val.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); val.setVerticalAlignment(PdfPCell.ALIGN_CENTER); table.addCell(val); }
From source file:ispyb.client.biosaxs.pdf.DataAcquisitionPDFReport.java
License:Open Source License
private Element getImageTable(Experiment3VO experiment, List<Buffer3VO> buffers) { PdfPTable table = new PdfPTable(ImageColumns.length); table.setWidthPercentage(100f);/*from w ww.jav a2 s . c o m*/ table.getDefaultCell().setPadding(3); table.getDefaultCell().setColspan(ImageColumns.length); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); table.getDefaultCell().setColspan(1); table.getDefaultCell().setBackgroundColor(PdfRtfExporter.BLUE_COLOR); for (int i = 0; i < ImageColumns.length; i++) { table.addCell(this.getCell(ImageColumns[i], BaseFont.HELVETICA_BOLD)); } table.getDefaultCell().setBackgroundColor(null); List<Measurement3VO> measurements = experiment.getMeasurements(); Collections.sort(measurements, MeasurementComparator .compare(MeasurementComparator.getComparator(MeasurementComparator.PRIOTIRY_SORT_ASC))); for (int x = 0; x < measurements.size(); x++) { Measurement3VO measurement = measurements.get(x); Specimen3VO specimen = experiment.getSampleById(measurement.getSpecimenId()); if (specimen.getMacromolecule3VO() != null) { SaxsDataCollection3VO dataCollection = experiment .getDataCollectionByMeasurementId(measurement.getMeasurementId()); ArrayList<String> list = this.addAnalysisRow(measurement, experiment, buffers); if (list.size() > 0) { int sizeTable = list.get(0).length(); PdfPTable nested1 = new PdfPTable(sizeTable); nested1.getDefaultCell().setBorder(0); for (int i = 0; i < list.size(); i++) { if (i == 0) { nested1.getDefaultCell().setBackgroundColor(PdfRtfExporter.LIGHT_GREY_COLOR); nested1.getDefaultCell().setBorder(0); Paragraph cell = this.getCell(this.COLUMNS[i] + ": " + list.get(i)); nested1.addCell(cell); } else { nested1.getDefaultCell().setBackgroundColor(null); nested1.getDefaultCell().setBorder(0); PdfPCell cell = new PdfPCell(this.getCell(this.COLUMNS[i] + ": " + list.get(i))); cell.setBorder(0); nested1.addCell(cell); } } table.addCell(new PdfPCell(nested1)); if (dataCollection.getSubstraction3VOs() != null) { if (dataCollection.getSubstraction3VOs().size() > 0) { Subtraction3VO substraction = ((Subtraction3VO) (dataCollection.getSubstraction3VOs() .toArray()[0])); String scatteringImage = BiosaxsActions .checkFilePathForDevelopment(substraction.getScatteringFilePath()); if (scatteringImage != null) { if (new File(scatteringImage).exists()) { try { PdfPCell cell = new PdfPCell(Image.getInstance(scatteringImage), true); cell.setBorder(0); table.addCell(cell); } catch (Exception e) { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); e.printStackTrace(); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } String guinierImage = BiosaxsActions .checkFilePathForDevelopment(substraction.getGuinierFilePath()); if (guinierImage != null) { if (new File(guinierImage).exists()) { try { PdfPCell cell = new PdfPCell(Image.getInstance(guinierImage), true); cell.setBorder(0); table.addCell(cell); } catch (Exception e) { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); e.printStackTrace(); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } String kraktyImage = BiosaxsActions .checkFilePathForDevelopment(substraction.getKratkyFilePath()); if (kraktyImage != null) { if (new File(kraktyImage).exists()) { try { PdfPCell cell = new PdfPCell(Image.getInstance(kraktyImage), true); cell.setBorder(0); table.addCell(cell); } catch (Exception e) { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); e.printStackTrace(); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } String gnomImage = BiosaxsActions .checkFilePathForDevelopment(substraction.getGnomFilePath()); if (gnomImage != null) { if (new File(gnomImage).exists()) { try { PdfPCell cell = new PdfPCell(Image.getInstance(gnomImage), true); cell.setBorder(0); table.addCell(cell); } catch (Exception e) { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); e.printStackTrace(); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } else { PdfPCell cell = new PdfPCell(this.getCell("Image not found")); cell.setBorder(0); table.addCell(cell); } } } } } } return table; }
From source file:it.eng.spagobi.engines.documentcomposition.exporterUtils.PdfCreator.java
License:Mozilla Public License
public FileOutputStream createPdfFile(FileOutputStream fileOutputStream, Map<String, DocumentContainer> documentsMap, boolean defaultStyle) throws MalformedURLException, IOException, DocumentException { logger.debug("IN"); Document document = new Document(PageSize.A4.rotate()); Rectangle rect = document.getPageSize(); docWidth = rect.getWidth();/*w ww . ja v a 2 s . c o m*/ docHeight = rect.getHeight(); logger.debug("document size width: " + docWidth + " height: " + docHeight); //PdfWriter writer=PdfWriter.getInstance(document,new FileOutputStream("C:/comp/SpagoBIProva.pdf")); PdfWriter writer = PdfWriter.getInstance(document, fileOutputStream); document.open(); int documentsNumber = documentsMap.keySet().size(); int columnnsNumber = 2; if (defaultStyle == true) { logger.debug("use default style"); int cellsCounter = 0; PdfPTable table = new PdfPTable(columnnsNumber); table.setWidthPercentage(100); for (Iterator iterator = documentsMap.keySet().iterator(); iterator.hasNext();) { String label = (String) iterator.next(); DocumentContainer docContainer = documentsMap.get(label); byte[] content = docContainer.getContent(); if (content != null) { Image img = null; try { img = Image.getInstance(content); table.addCell(img); } catch (Exception e) { logger.debug("Trying to evaluate response as a PDF file... "); table.addCell(""); // try { // PdfReader reader = new PdfReader(content); // PdfImportedPage page = writer.getImportedPage(reader, 1); // writer.addPage(page); // table.addCell(""); // } catch (Exception x) { // logger.error("Error in inserting image for document " + label, e); // logger.error("Error in inserting pdf file for document " + label, x); // table.addCell(""); // } } } cellsCounter++; } // if cell counter is not pair make it pair if (cellsCounter % 2 != 0) { table.addCell(""); } document.add(table); } else { // ************* NO DEFAULT STYLE ***************** logger.debug("No default style"); // I want to calculate total height of scaled heights!! //int totalScaledHeight=calculateTotaleScaledHeights(documentsMap, defaultStyle); // run on all documents for (Iterator iterator = documentsMap.keySet().iterator(); iterator.hasNext();) { String label = (String) iterator.next(); logger.debug("document with label " + label); DocumentContainer docContainer = documentsMap.get(label); MetadataStyle style = docContainer.getStyle(); // one table for each image, set at absolute position PdfPTable table = new PdfPTable(1); // width and height specified for the container by style attribute int widthStyle = style.getWidth(); int heightStyle = style.getHeight(); logger.debug("style for document width: " + widthStyle + " height: " + heightStyle); // width and height for the table scaled to the document size int tableWidth = calculatePxSize(docWidth, widthStyle, videoWidth); int tableHeight = calculatePxSize(docHeight, heightStyle, videoHeight); logger.debug("table for document width: " + tableWidth + " height: " + tableHeight); // x and y position as specified for the container by the style attribute int yStyle = style.getY(); int xStyle = style.getX(); // width and height scaled to the document size int xPos = (calculatePxPos(docWidth, xStyle, videoWidth)); int yPos = (int) docHeight - (calculatePxPos(docHeight, yStyle, videoHeight)); logger.debug("Table position at x: " + xPos + " y: " + yPos); // get the image byte[] content = docContainer.getContent(); if (content != null) { Image img = null; try { img = Image.getInstance(content); } catch (Exception e) { logger.debug("Trying to evaluate response as a PDF file... "); try { PdfReader reader = new PdfReader(content); PdfContentByte cb = writer.getDirectContent(); PdfImportedPage page = writer.getImportedPage(reader, 1); float[] tm = getTransformationMatrix(page, xPos, yPos, tableWidth, tableHeight); cb.addTemplate(page, tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]); } catch (Exception x) { logger.error("Error in inserting image for document " + label, e); logger.error("Error in inserting pdf file for document " + label, x); } continue; } //if it is a REPORT and has more than one page, too large, you have to resize the image, but how to understand it? // if image size is more than double of the container size cut the first part,otherwise scale it if (docContainer.getDocumentType().equals("REPORT")) { boolean cutImageWIdth = isToCutWidth(img, tableWidth); boolean cutImageHeight = isToCutHeight(img, tableWidth); if (cutImageWIdth == true || cutImageHeight == true) { logger.debug( "Report will be cut to width " + tableWidth + " and height " + tableHeight); try { img = cutImage(content, cutImageHeight, cutImageWIdth, tableHeight, tableWidth, (int) img.getWidth(), (int) img.getHeight()); } catch (Exception e) { logger.error( "Error in image cut, cutt will be ignored and image will be drawn anyway ", e); } } } // this is percentage to resize // The image must be size within the cell int percToResize = percentageToResize((int) img.getWidth(), (int) img.getHeight(), tableWidth, tableHeight); logger.debug("image will be scaled of percentage " + percToResize); img.scalePercent(percToResize); PdfPCell cell = new PdfPCell(img); cell.setNoWrap(true); cell.setFixedHeight(tableHeight); cell.setBorderWidth(0); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_CENTER); table.addCell(cell); //table.setWidthPercentage(tableWidthPerc); table.setTotalWidth(tableWidth); table.setLockedWidth(true); } else { // TODO: setALT! } logger.debug("Add table"); table.writeSelectedRows(0, -1, xPos, yPos, writer.getDirectContent()); logger.debug("Document added"); } } document.close(); logger.debug("OUT"); return fileOutputStream; }