Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.asptt.plongee.resa.util; import com.asptt.plongee.resa.model.Adherent; import com.asptt.plongee.resa.model.FicheSecurite; import com.asptt.plongee.resa.model.Palanque; import com.asptt.plongee.resa.model.Plongee; import com.asptt.plongee.resa.wicket.ResaSession; import com.asptt.plongee.resa.wicket.page.admin.fichesecurite.UpdateListeFS; import com.itextpdf.io.IOException; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants; import com.itextpdf.io.font.FontConstants; import com.itextpdf.kernel.color.Color; import com.itextpdf.kernel.color.DeviceRgb; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfNumber; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.Style; import com.itextpdf.layout.border.Border; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.element.Text; import com.itextpdf.layout.property.HorizontalAlignment; import com.itextpdf.layout.property.TextAlignment; import com.itextpdf.layout.property.VerticalAlignment; import java.io.FileNotFoundException; import java.util.List; import java.util.StringTokenizer; import java.util.logging.Level; import org.apache.wicket.Resource; import org.apache.wicket.markup.html.WebResource; import org.apache.wicket.markup.html.link.PopupSettings; import org.apache.wicket.markup.html.link.ResourceLink; import org.apache.wicket.util.resource.FileResourceStream; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.ResourceStreamNotFoundException; /** * * @author ECUS6396 */ public class UtilsFSpdf { public static final String DEST = "generatedPdf/toto.pdf"; public static final String catalinaBasePath = ResaSession.get().getCATALINA_PATH(); /* Constants form itext5 */ public static final PdfNumber INVERTEDPORTRAIT = new PdfNumber(180); public static final PdfNumber LANDSCAPE = new PdfNumber(90); public static final PdfNumber PORTRAIT = new PdfNumber(0); public static final PdfNumber SEASCAPE = new PdfNumber(270); /** * * @param args * @throws IOException * @throws FileNotFoundException * @throws java.io.IOException */ public static void main(String args[]) throws IOException, FileNotFoundException, java.io.IOException { // File file = new File(DEST); // file.getParentFile().mkdirs(); // new UtilsFSpdf().createTable(DEST); // FicheSecurite fs = new FicheSecurite(); // fs.setSite("Le Chaouen"); // new UtilsFSpdf().createPdf(DEST, fs); StringTokenizer str = new StringTokenizer("AAAA BBBB CCCC DDDD"); String lastToken = "?"; String nom = ""; int nbtoken = str.countTokens(); System.out.println("Nombre de token=" + nbtoken); for (int i = 1; i <= nbtoken; i++) { String token = str.nextToken(); if (i == nbtoken) { lastToken = token; System.out.println("i=" + i + ", lastToken=" + lastToken); } else { nom = nom + token + " "; System.out.println("i=" + i + ", nom=" + nom); } } System.out.println("nom=" + nom); System.out.println("last token=" + lastToken); } public Document createPdfFS(String dest, FicheSecurite fs) throws IOException, FileNotFoundException, java.io.IOException { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); PageSize ps = PageSize.A4.rotate(); //calcul du nombre de page (si + de 8 palanques => 2 pages) int nbPage = 1; if (fs.getPalanques().size() > 8) { nbPage = 2; } pdfDoc.addNewPage(ps); Document doc = new Document(pdfDoc); Table entete = createEntete(fs, 1, nbPage); doc.add(entete); // style pour les plongeurs Style s_plongeur = new Style(); PdfFont fontPlongeur = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); s_plongeur.setFont(fontPlongeur).setFontSize(10); s_plongeur.setFontColor(Color.BLACK); // Deuxieme table pour les palanques Table tablePalanques = new Table(4); for (int i = 0; i < 8; i++) { createTablePlongeur(tablePalanques, fs, i, s_plongeur); } doc.add(tablePalanques); if (nbPage == 2) { Table tablePalanques2 = new Table(4); pdfDoc.addNewPage(ps); Table entete2 = createEntete(fs, 2, nbPage); doc.add(entete2); for (int i = 8; i < 16; i++) { createTablePlongeur(tablePalanques2, fs, i, s_plongeur); } doc.add(tablePalanques2); } doc.close(); return doc; } private Table createEntete(FicheSecurite fs, int numeroPage, int nbPage) throws java.io.IOException { float[] columnWidths = { 20, 27, 23, 23, 6 }; // table : premiere table pour les parametres de la plonge Table table = new Table(columnWidths); table.setMargins(0, 0, 0, 0); table.setWidthPercent(100); // entete style pour les entetes Style entete = new Style(); PdfFont fontEntete = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); entete.setFont(fontEntete).setFontSize(12); entete.setFontColor(Color.BLACK); // style pour les libells Style libelle = new Style(); PdfFont fontLibelle = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); libelle.setFont(fontLibelle).setFontSize(12); libelle.setFontColor(Color.RED); Paragraph entete1 = new Paragraph(); entete1.add(new Text("DATE\n").addStyle(entete)); entete1.add(new Text("DIRECTEUR DE PLONGEE\n").addStyle(entete)); entete1.add(new Text("PILOTE DU BATEAU\n").addStyle(entete)); entete1.add(new Text("LIEU DE PLONGEE\n").addStyle(entete)); entete1.add(new Text("METEO\n").addStyle(entete)); Paragraph libelle1 = new Paragraph(); libelle1.add(new Text(": " + ResaUtil.getDateString(fs.getDatePlongee()) + "\n").addStyle(libelle)); libelle1.add(new Text(": " + fs.getNomDP() + "\n").addStyle(libelle)); libelle1.add(new Text(": " + fs.getNomPilote() + "\n").addStyle(libelle)); libelle1.add(new Text(": " + fs.getSite() + "\n").addStyle(libelle)); libelle1.add(new Text(": " + fs.getMeteo() + "\n").addStyle(libelle)); Cell cellEntete1 = new Cell(); cellEntete1.add(entete1); cellEntete1.setBorderRight(Border.NO_BORDER); cellEntete1.setHorizontalAlignment(HorizontalAlignment.LEFT); table.addCell(cellEntete1); Cell cellLibelle1 = new Cell(); cellLibelle1.add(libelle1); cellLibelle1.setBorderLeft(Border.NO_BORDER); cellLibelle1.setTextAlignment(TextAlignment.LEFT); table.addCell(cellLibelle1); Paragraph titre = new Paragraph(); titre.add(new Text("ASPTT MARSEILLE\n").addStyle(entete)); titre.add(new Text("PLONGEE\n").addStyle(entete)); titre.add(new Text("---\n").addStyle(entete)); titre.add(new Text("Fiche de Scurit\n").addStyle(entete)); Cell cellTitre = new Cell(); cellTitre.add(titre); cellTitre.setTextAlignment(TextAlignment.CENTER); cellTitre.setVerticalAlignment(VerticalAlignment.MIDDLE); table.addCell(cellTitre); Paragraph entete2 = new Paragraph(); entete2.add(new Text("PLONGEE NUMERO\n").addStyle(entete)); entete2.add(new Text("NOMBRE DE PLONGEURS\n").addStyle(entete)); entete2.add(new Text("HEURE DE DEPART\n").addStyle(entete)); entete2.add(new Text("HEURES BATEAU\n").addStyle(entete)); entete2.add(new Text("Page \n").addStyle(entete)); Paragraph libelle2 = new Paragraph(); libelle2.add(new Text(": " + fs.getNumeroPlongee() + "\n").addStyle(libelle)); libelle2.add(new Text(": " + fs.getNbPlongeurs() + "\n").addStyle(libelle)); libelle2.add(new Text(": " + fs.getHhDepart() + ":" + fs.getMnDepart() + "\n").addStyle(libelle)); libelle2.add(new Text(": " + fs.getNbHeuresBateau() + "\n").addStyle(libelle)); libelle2.add(new Text(": " + numeroPage + " / " + nbPage + "\n").addStyle(libelle)); Cell cellEntete2 = new Cell(); cellEntete2.add(entete2); cellEntete2.setBorderRight(Border.NO_BORDER); cellEntete2.setHorizontalAlignment(HorizontalAlignment.RIGHT); table.addCell(cellEntete2); Cell cellLibelle2 = new Cell(); cellLibelle2.add(libelle2); cellLibelle2.setBorderLeft(Border.NO_BORDER); cellLibelle2.setTextAlignment(TextAlignment.LEFT); table.addCell(cellLibelle2); return table; } private void createTablePlongeur(Table tablePalanques, FicheSecurite fs, int i, Style s_plongeur) { float[] colWidths = { 25, 25, 25, 25 }; Table tablePlongeurs = new Table(colWidths); tablePlongeurs.setMarginTop(0); tablePlongeurs.setMarginBottom(0); tablePlongeurs.setWidthPercent(100); // first row Cell cellNom = new Cell(1, 3).add(new Paragraph("Nom")); cellNom.setTextAlignment(TextAlignment.CENTER); cellNom.setFontSize(12); cellNom.setPadding(5); cellNom.setBackgroundColor(new DeviceRgb(140, 221, 8)); tablePlongeurs.addCell(cellNom); Cell cellApt = new Cell(1, 1).add(new Paragraph("Apt")); cellApt.setTextAlignment(TextAlignment.CENTER); cellApt.setFontSize(12); cellApt.setPadding(5); cellApt.setBackgroundColor(new DeviceRgb(140, 221, 8)); tablePlongeurs.addCell(cellApt); if (fs.getPalanques().size() > i) { initTablePlongeur(fs.getPalanques().get(i), tablePlongeurs, s_plongeur); } else { initTablePlongeur(null, tablePlongeurs, s_plongeur); } tablePalanques.addCell(tablePlongeurs); } private void initTablePlongeur(Palanque palanque, Table tablePlongeurs, Style s_plongeur) { if (null == palanque) { palanque = new Palanque(); palanque.setId(0); palanque.setNomCompletGuide(""); palanque.setNomCompletPlongeur1(""); palanque.setNomCompletPlongeur2(""); palanque.setNomCompletPlongeur3(""); palanque.setNomCompletPlongeur4(""); palanque.setAptGuide(""); palanque.setAptPlongeur1(""); palanque.setAptPlongeur2(""); palanque.setAptPlongeur3(""); palanque.setAptPlongeur4(""); palanque.setDureeTotalePrevue(0); palanque.setDureeTotaleRea(0); palanque.setProfondeurMaxPrevue(0); palanque.setProfondeurMaxRea(0); palanque.setPalier3m(0); palanque.setPalier6m(0); palanque.setPalier9m(0); } Paragraph libNomPlongeur = new Paragraph(); // libNomPlongeur.add(new Text("IdPalanque : \n").addStyle(s_plongeur)); libNomPlongeur.add(new Text("Guide : " + getLibellePlongeur(palanque.getNomCompletGuide())[0] + "\n").addStyle(s_plongeur)); libNomPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletPlongeur1())[0] + "\n")).addStyle(s_plongeur); libNomPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletPlongeur2())[0] + "\n")).addStyle(s_plongeur); libNomPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletPlongeur3())[0] + "\n")).addStyle(s_plongeur); libNomPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletPlongeur4())[0] + "\n")).addStyle(s_plongeur); Cell cellNomPlongeur = new Cell(1, 3); cellNomPlongeur.add(libNomPlongeur); cellNomPlongeur.setBorderRight(Border.NO_BORDER); cellNomPlongeur.setHorizontalAlignment(HorizontalAlignment.LEFT); tablePlongeurs.addCell(cellNomPlongeur); Paragraph libAptPlongeur = new Paragraph(); libAptPlongeur.setMarginLeft(10); // libAptPlongeur.add(new Text(palanque.getId().toString()+ "\n").addStyle(s_plongeur)); libAptPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletGuide())[1] + "\n").addStyle(s_plongeur)); libAptPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletPlongeur1())[1] + "\n")).addStyle(s_plongeur); libAptPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletPlongeur2())[1] + "\n")).addStyle(s_plongeur); libAptPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletPlongeur3())[1] + "\n")).addStyle(s_plongeur); libAptPlongeur.add(new Text(getLibellePlongeur(palanque.getNomCompletPlongeur4())[1] + "\n")).addStyle(s_plongeur); Cell cellAptPlongeur = new Cell(1, 1); cellAptPlongeur.add(libAptPlongeur); cellAptPlongeur.setBorderRight(Border.NO_BORDER); cellAptPlongeur.setHorizontalAlignment(HorizontalAlignment.RIGHT); tablePlongeurs.addCell(cellAptPlongeur); //Profondeur Cell cellProfondeurMaxi = new Cell(2, 2); cellProfondeurMaxi.add(new Paragraph("Profondeur Maxi")).addStyle(s_plongeur); cellProfondeurMaxi.setBorderRight(Border.NO_BORDER); cellProfondeurMaxi.setHorizontalAlignment(HorizontalAlignment.CENTER); cellProfondeurMaxi.setVerticalAlignment(VerticalAlignment.MIDDLE); tablePlongeurs.addCell(cellProfondeurMaxi); Cell cellPPrevue = new Cell(1, 1); cellPPrevue.add(new Paragraph("Prvue")).addStyle(s_plongeur); cellPPrevue.setBorderRight(Border.NO_BORDER); cellPPrevue.setHorizontalAlignment(HorizontalAlignment.CENTER); tablePlongeurs.addCell(cellPPrevue); Cell cellPrevueFS = new Cell(1, 1); String profPrevue = Integer.toString(palanque.getProfondeurMaxPrevue()); Paragraph p_ProfPrevue = new Paragraph(); p_ProfPrevue.add(profPrevue); cellPrevueFS.add(p_ProfPrevue).addStyle(s_plongeur); cellPrevueFS.setBorderRight(Border.NO_BORDER); cellPrevueFS.setHorizontalAlignment(HorizontalAlignment.CENTER); tablePlongeurs.addCell(cellPrevueFS); Cell cellPRea = new Cell(1, 1); cellPRea.add(new Paragraph("Ra")).addStyle(s_plongeur); cellPRea.setBorderRight(Border.NO_BORDER); cellPRea.setHorizontalAlignment(HorizontalAlignment.CENTER); tablePlongeurs.addCell(cellPRea); Cell cellReaFS = new Cell(1, 1); String profRee = Integer.toString(palanque.getProfondeurMaxRea()); Paragraph p_ProfRea = new Paragraph(); p_ProfRea.add(profRee); cellReaFS.add(p_ProfRea).addStyle(s_plongeur); cellReaFS.setBorderRight(Border.NO_BORDER); cellReaFS.setHorizontalAlignment(HorizontalAlignment.CENTER); tablePlongeurs.addCell(cellReaFS); //Dure Cell cellDureMaxi = new Cell(2, 2); cellDureMaxi.add(new Paragraph("Dure Totale")).addStyle(s_plongeur); cellDureMaxi.setBorderRight(Border.NO_BORDER); cellDureMaxi.setHorizontalAlignment(HorizontalAlignment.CENTER); cellDureMaxi.setVerticalAlignment(VerticalAlignment.MIDDLE); tablePlongeurs.addCell(cellDureMaxi); Cell cellDPrevue = new Cell(1, 1); cellDPrevue.add(new Paragraph("Prvue")).addStyle(s_plongeur); cellDPrevue.setBorderRight(Border.NO_BORDER); cellDPrevue.setHorizontalAlignment(HorizontalAlignment.LEFT); tablePlongeurs.addCell(cellDPrevue); Cell cellDureeFS = new Cell(1, 1); String dureePrevue = Integer.toString(palanque.getDureeTotalePrevue()); Paragraph p_DureePrevue = new Paragraph(); p_DureePrevue.add(dureePrevue); cellDureeFS.add(p_DureePrevue).addStyle(s_plongeur); cellDureeFS.setBorderRight(Border.NO_BORDER); cellDureeFS.setHorizontalAlignment(HorizontalAlignment.CENTER); tablePlongeurs.addCell(cellDureeFS); Cell cellDRea = new Cell(1, 1); cellDRea.add(new Paragraph("Ra")).addStyle(s_plongeur); cellDRea.setBorderRight(Border.NO_BORDER); cellDRea.setHorizontalAlignment(HorizontalAlignment.LEFT); tablePlongeurs.addCell(cellDRea); Cell cellDureeReaFS = new Cell(1, 1); String dureeRea = Integer.toString(palanque.getDureeTotaleRea()); Paragraph p_DureeRea = new Paragraph(); p_DureeRea.add(dureeRea); cellDureeReaFS.add(p_DureeRea).addStyle(s_plongeur); cellDureeReaFS.setBorderRight(Border.NO_BORDER); cellDureeReaFS.setHorizontalAlignment(HorizontalAlignment.CENTER); tablePlongeurs.addCell(cellDureeReaFS); //PALIERS Cell cellPalier = new Cell(1, 1); Paragraph p_palier = new Paragraph(); p_palier.add("Paliers"); cellPalier.add(p_palier).addStyle(s_plongeur); cellPalier.setBorderRight(Border.NO_BORDER); cellPalier.setHorizontalAlignment(HorizontalAlignment.LEFT); tablePlongeurs.addCell(cellPalier); Cell cellPalier3 = new Cell(1, 1); Paragraph p_palier3 = new Paragraph(); p_palier3.add("3m / " + palanque.getPalier3m()); cellPalier3.add(p_palier3).addStyle(s_plongeur); cellPalier3.setBorderRight(Border.NO_BORDER); cellPalier3.setHorizontalAlignment(HorizontalAlignment.LEFT); tablePlongeurs.addCell(cellPalier3); Cell cellPalier6 = new Cell(1, 1); Paragraph p_palier6 = new Paragraph(); p_palier6.add("6m / " + palanque.getPalier6m()); cellPalier6.add(p_palier6).addStyle(s_plongeur); cellPalier6.setBorderRight(Border.NO_BORDER); cellPalier6.setHorizontalAlignment(HorizontalAlignment.LEFT); tablePlongeurs.addCell(cellPalier6); Cell cellPalier9 = new Cell(1, 1); Paragraph p_palier9 = new Paragraph(); p_palier9.add("9m / " + palanque.getPalier9m()); cellPalier9.add(p_palier9).addStyle(s_plongeur); cellPalier9.setBorderRight(Border.NO_BORDER); cellPalier9.setHorizontalAlignment(HorizontalAlignment.LEFT); tablePlongeurs.addCell(cellPalier9); } private String[] getLibellePlongeur(String libAFormater) { String[] result = new String[2]; if (null == libAFormater || libAFormater.equals("")) { result[0] = " "; result[1] = " "; } else { StringTokenizer str = new StringTokenizer(libAFormater); String aptitude = ""; String nom = ""; int nbtoken = str.countTokens(); for (int i = 1; i <= nbtoken; i++) { String token = str.nextToken(); if (i == nbtoken) { aptitude = token; } else { nom = nom + token + " "; } } result[0] = nom; result[1] = aptitude; } // System.out.println("RESULT nom="+result[0]+" aptitude="+result[1]); return result; } public void createPdf(String dest, FicheSecurite fs) throws IOException, FileNotFoundException, java.io.IOException { //Initialize PDF writer PdfWriter writer = new PdfWriter(dest); //Initialize PDF document PdfDocument pdf = new PdfDocument(writer); // Initialize document Document document = new Document(pdf); //Add paragraph to the document document.add(new Paragraph(fs.getSite())); // Add content Text title1 = new Text("The Strange Case of ").setFontColor(Color.BLUE); Text title2 = new Text("Dr. Jekyll SIIMMOONN").setStrokeColor(Color.GREEN) .setTextRenderingMode(PdfCanvasConstants.TextRenderingMode.FILL_STROKE); Text title3 = new Text(" and "); Text title4 = new Text("Mr. Hyde").setStrokeColor(Color.RED).setStrokeWidth(0.5f) .setTextRenderingMode(PdfCanvasConstants.TextRenderingMode.STROKE); Paragraph p = new Paragraph().setFontSize(24).add(title1).add(title2).add(title3).add(title4); document.add(p); Style normal = new Style(); PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); normal.setFont(font).setFontSize(14); Style code = new Style(); PdfFont monospace = PdfFontFactory.createFont(FontConstants.COURIER); code.setFont(monospace).setFontColor(Color.RED).setBackgroundColor(Color.LIGHT_GRAY); Paragraph p2 = new Paragraph(); p2.add(new Text("The Strange Case of ").addStyle(normal)); p2.add(new Text("Dr. Jekyll").addStyle(code)); p2.add(new Text(" and ").addStyle(normal)); p2.add(new Text("Mr. Hyde").addStyle(code)); p2.add(new Text(".").addStyle(normal)); document.add(p2); //Close document document.close(); // PageSize ps = PageSize.A4.rotate(); // PdfPage page = pdf.addNewPage(ps); // PdfCanvas canvas = new PdfCanvas(page); // canvas.concatMatrix(1, 0, 0, 1, ps.getWidth() / 2, ps.getHeight() / 2); ////Draw X axis // canvas.moveTo(-(ps.getWidth() / 2 - 15), 0) // .lineTo(ps.getWidth() / 2 - 15, 0) // .stroke(); ////Draw X axis arrow // canvas.setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND) // .moveTo(ps.getWidth() / 2 - 25, -10) // .lineTo(ps.getWidth() / 2 - 15, 0) // .lineTo(ps.getWidth() / 2 - 25, 10).stroke() // .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.MITER); ////Draw Y axis // canvas.moveTo(0, -(ps.getHeight() / 2 - 15)) // .lineTo(0, ps.getHeight() / 2 - 15) // .stroke(); ////Draw Y axis arrow // canvas.saveState() // .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND) // .moveTo(-10, ps.getHeight() / 2 - 25) // .lineTo(0, ps.getHeight() / 2 - 15) // .lineTo(10, ps.getHeight() / 2 - 25).stroke() // .restoreState(); ////Draw X serif // for (int i = -((int) ps.getWidth() / 2 - 61); // i < ((int) ps.getWidth() / 2 - 60); i += 40) { // canvas.moveTo(i, 5).lineTo(i, -5); // } ////Draw Y serif // for (int j = -((int) ps.getHeight() / 2 - 57); // j < ((int) ps.getHeight() / 2 - 56); j += 40) { // canvas.moveTo(5, j).lineTo(-5, j); // } // canvas.stroke(); //// pdf.close(); //// PdfPage page = pdf.addNewPage(); //// Rectangle rectangle = new Rectangle(x, y, 100, 100); //// Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle); // float llx = 0; // float lly = 0; // float urx = 100; // float ury = 100; // for (Palanque palanque : fs.getPalanques()) { // llx=llx+5; // lly=lly+70; // Rectangle rectangle = new Rectangle(llx, lly, urx-llx, ury-lly); //// PdfCanvas canvas = new PdfCanvas(page); // canvas = new PdfCanvas(page); //// PdfCanvas canvas = new PdfCanvas(pdf.addNewPage()); //// pdfCanvas.rectangle(rectangle); // canvas.setStrokeColor(Color.RED).setLineWidth(0.5f).rectangle(rectangle).stroke(); //// canvas = new Canvas(pdfCanvas, pdf, rectangle); //// pdfCanvas.stroke(); // PdfFont fontRoman = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); // PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD); // Text title = new Text("GUIDE palanque:"+palanque.getNomCompletGuide()).setFont(bold); // Text author = new Text("PLONGEUR 1 palanque:"+palanque.getNomCompletPlongeur1()).setFont(fontRoman); // Paragraph p3 = new Paragraph().add(title).add(" by ").add(author); // new Canvas(canvas,pdf,rectangle).add(p3); //// canvas.add(p3); // } //Close document pdf.close(); } public void createPdfPlongee(String dest, Plongee plongee) throws IOException, FileNotFoundException, java.io.IOException { PageSize ps = PageSize.A4.rotate(); PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); pdfDoc.addNewPage(ps); Document doc = new Document(pdfDoc); float[] columnWidths = { 20, 20, 5, 10, 15, 15, 15 }; // table : premiere table pour les parametres de la plonge Table table = new Table(columnWidths); table.setMargins(0, 0, 0, 0); table.setWidthPercent(100); // entete style pour les titres Style s_titre = new Style(); PdfFont fontEntete = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); s_titre.setFont(fontEntete).setFontSize(12); s_titre.setFontColor(Color.ORANGE); // style pour l'entete de la table Style s_entete = new Style(); PdfFont fontLibelle = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN); s_entete.setFont(fontLibelle).setFontSize(12); s_entete.setFontColor(Color.BLUE); Paragraph entete1 = new Paragraph(); entete1.add(new Text("Plong\u00e9e du " + ResaUtil.getJourDatePlongee(plongee.getDatePlongee()) + " '" + ResaUtil.getHeurePlongee(plongee.getDatePlongee()) + "'\n").addStyle(s_titre)); entete1.add( new Text("Nombre de participants " + plongee.getParticipants().size() + "\n").addStyle(s_titre)); entete1.add(new Text("\n").addStyle(s_titre)); entete1.add(new Text("Inscrit\n").addStyle(s_titre)); Cell cellEntete1 = new Cell(1, 7); cellEntete1.add(entete1); cellEntete1.setBorderRight(Border.NO_BORDER); // cellEntete1.setBackgroundColor(Color.BLUE); cellEntete1.setHorizontalAlignment(HorizontalAlignment.LEFT); table.addCell(cellEntete1); Cell cellNom = new Cell().add(new Paragraph("Nom")); cellNom.setTextAlignment(TextAlignment.CENTER); cellNom.setFontSize(8); cellNom.setFontColor(Color.BLACK); cellNom.setPadding(5); cellNom.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellNom); Cell cellPrenom = new Cell().add(new Paragraph("Prnom")); cellPrenom.setTextAlignment(TextAlignment.CENTER); cellPrenom.setFontSize(8); cellPrenom.setFontColor(Color.BLACK); cellPrenom.setPadding(5); cellPrenom.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellPrenom); Cell cellNiveau = new Cell().add(new Paragraph("Niveau")); cellNiveau.setTextAlignment(TextAlignment.CENTER); cellNiveau.setFontSize(8); cellNiveau.setFontColor(Color.BLACK); cellNiveau.setPadding(5); cellNiveau.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellNiveau); Cell cellAptitude = new Cell().add(new Paragraph("Aptitude")); cellAptitude.setTextAlignment(TextAlignment.CENTER); cellAptitude.setFontSize(8); cellAptitude.setFontColor(Color.BLACK); cellAptitude.setPadding(5); cellAptitude.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellAptitude); Cell cellTelephone1 = new Cell().add(new Paragraph("Tlphone")); cellTelephone1.setTextAlignment(TextAlignment.CENTER); cellTelephone1.setFontSize(8); cellTelephone1.setFontColor(Color.BLACK); cellTelephone1.setPadding(5); cellTelephone1.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellTelephone1); Cell cellCommentaire = new Cell().add(new Paragraph("Commentaire")); cellCommentaire.setTextAlignment(TextAlignment.CENTER); cellCommentaire.setFontSize(8); cellCommentaire.setFontColor(Color.BLACK); cellCommentaire.setPadding(5); cellCommentaire.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellCommentaire); List<Adherent> adherentsInscrit = plongee.getParticipants(); for (Adherent adherent : adherentsInscrit) { Cell cellNomP = new Cell().add(new Paragraph(adherent.getNom())); cellNomP.setTextAlignment(TextAlignment.CENTER); cellNomP.setFontSize(8); cellNomP.setFontColor(Color.BLACK); cellNomP.setPadding(5); cellNomP.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellNomP); Cell cellPrenomP = new Cell().add(new Paragraph(adherent.getPrenom())); cellPrenomP.setTextAlignment(TextAlignment.CENTER); cellPrenomP.setFontSize(8); cellPrenomP.setFontColor(Color.BLACK); cellPrenomP.setPadding(5); cellPrenomP.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellPrenomP); // Ds que le plongeur est encadrant, on affiche son niveau d'encadrement String niveauAffiche = adherent.getPrerogative(); // Pour les externes, le niveau est suffix par (Ext.) if (adherent.getActifInt() == 2) { niveauAffiche = niveauAffiche + " (Ext.)"; } Cell cellNiveauP = new Cell().add(new Paragraph(niveauAffiche)); cellNiveauP.setTextAlignment(TextAlignment.CENTER); cellNiveauP.setFontSize(8); cellNiveauP.setFontColor(Color.BLACK); cellNiveauP.setPadding(5); cellNiveauP.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellNiveauP); Cell cellAptitudeP; if (null == adherent.getAptitude()) { cellAptitudeP = new Cell().add(new Paragraph(" ")); } else { cellAptitudeP = new Cell().add(new Paragraph(adherent.getAptitude().getText())); } cellAptitudeP.setTextAlignment(TextAlignment.CENTER); cellAptitudeP.setFontSize(8); cellAptitudeP.setFontColor(Color.BLACK); cellAptitudeP.setPadding(5); cellAptitudeP.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellAptitudeP); Cell cellTelephone1P = new Cell().add(new Paragraph(adherent.getTelephone())); cellTelephone1P.setTextAlignment(TextAlignment.CENTER); cellTelephone1P.setFontSize(8); cellTelephone1P.setFontColor(Color.BLACK); cellTelephone1P.setPadding(5); cellTelephone1P.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellTelephone1P); Cell cellCommentaireP = new Cell().add(new Paragraph(adherent.getCommentaire())); cellCommentaireP.setTextAlignment(TextAlignment.CENTER); cellCommentaireP.setFontSize(8); cellCommentaireP.setFontColor(Color.BLACK); cellCommentaireP.setPadding(5); cellCommentaireP.setBackgroundColor(new DeviceRgb(140, 221, 8)); table.addCell(cellCommentaireP); } doc.add(table); doc.close(); } public ResourceLink createWebResourcePdf(final Adherent adherent, final FicheSecurite fs) { Resource pdfResource = new WebResource() { @Override public IResourceStream getResourceStream() { String nom = adherent.getNom(); final String realPath; if (null != fs) { realPath = catalinaBasePath.concat(Parameters.getString("fs.path")).concat(nom).concat("_FS") .concat(".pdf"); // Cration du pdf try { createPdfFS(realPath, fs); } catch (com.itextpdf.io.IOException ex) { java.util.logging.Logger.getLogger(UpdateListeFS.class.getName()).log(Level.SEVERE, null, ex); } catch (java.io.IOException ex) { java.util.logging.Logger.getLogger(UpdateListeFS.class.getName()).log(Level.SEVERE, null, ex); } } else { //Recherche du Certificat mdical nom = nom.substring(0, 1).toUpperCase() + nom.substring(1).toLowerCase(); String license = adherent.getNumeroLicense(); realPath = catalinaBasePath.concat(Parameters.getString("cm.path")).concat(nom).concat("_") .concat(license).concat(".pdf"); } java.io.File file = new java.io.File(realPath); // created FileResourceStream object by passing the above File object name "pdfFile". IResourceStream stream = new FileResourceStream(file); file.deleteOnExit(); //finally returns the stream return stream; } }; /* * Created PopupSettings object named "popupSettings" by passing some parameters in the * constructor and also setted the width and height for popup window. */ PopupSettings popupSettings = new PopupSettings(PopupSettings.RESIZABLE | PopupSettings.SCROLLBARS) .setHeight(500).setWidth(700); /* * Created ResourceLink object named "resourceLink" by passing above Resource object * named "pdfResource" as second parameters.The first parameter is "wicket:id" by which * markup identifies the component and renders it on web page. */ ResourceLink resourceLink = (ResourceLink) new ResourceLink("openPdf", pdfResource); //Setted the popupSettings properties of "resourceLink". resourceLink.setPopupSettings(popupSettings); //if file not found disable resourcelink in case of certificat mdical (fs == null) if (null == fs) { try { pdfResource.getResourceStream().getInputStream(); } catch (ResourceStreamNotFoundException ex) { resourceLink.setEnabled(false); } } //return resourceLink for added in page return resourceLink; } }