1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
Document document = new Document(PageSize.A4);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try{
PdfWriter.getInstance(document, baos);
document.addTitle("ddedejdejdizjiozjodijo");
document.addHeader("cczczrfzrfz", "frfrzefezrfrefer");
document.addCreationDate();
System.out.println();
document.open();
// Ajout de l'entête
Paragraph para1 = new Paragraph();
para1.add("Entete du fichier PDF");
HeaderFooter header = new HeaderFooter(para1,false);
header.setAlignment(HeaderFooter.ALIGN_CENTER);
document.setHeader(header);
// Ajout du pied de page
Paragraph para2 = new Paragraph();
para2.add("Pied de page du fichier PDF");
HeaderFooter footer = new HeaderFooter(para2,false);
footer.setAlignment(HeaderFooter.ALIGN_CENTER);
document.setFooter(footer);
Image img = Image.getInstance("Workspace/win/WebContent/images/zoomold.jpg");
img.scalePercent(100);
Phrase h1 = new Phrase("header 1");
Phrase h2 = new Phrase("header 2");
//HeaderFooter header = new HeaderFooter(h1,h2);
//document.setHeader(header);
Phrase f1 = new Phrase("footer 1");
Phrase f2 = new Phrase("footer 2");
//HeaderFooter footer = new HeaderFooter(f1,f2);
//document.setFooter(footer);
//document.add(header);
//document.add(footer);
Font fontRed = FontFactory.getFont(FontFactory.COURIER, 8, Font.NORMAL, new Color(255,0,0));
Font fontTitre = new Font(Font.HELVETICA,Font.DEFAULTSIZE,Font.BOLD);
Font fontLibelle = new Font(Font.HELVETICA,8,Font.NORMAL);
Phrase libNumTrans = new Phrase("Numéro de transaction :",fontLibelle);
Phrase intNumTrans = new Phrase(String.valueOf(numTransaction),fontLibelle);
Phrase titre = new Phrase("Photo du bien",fontRed);
PdfPTable table = new PdfPTable(2);
PdfPCell cell = new PdfPCell(new Paragraph(titreEdTransaction,fontTitre));
Cell celltest = new Cell(cell);
cell.setColspan(2);
cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
cell.setVerticalAlignment(Cell.ALIGN_CENTER);
celltest.setHeader(true);
table.addCell(cell);
table.addCell(libNumTrans);
table.addCell(intNumTrans);
table.addCell("Adresse : ");
table.addCell(numVoie.trim()+", "+typeVoie.trim()+" "+nomVoie.trim()+", "+codePostal.trim()+" "+commune.trim());
table.addCell("Négociateur : ");
table.addCell(prenomNego.trim()+" "+nomNego.trim());
table.addCell(titre);
table.addCell(img);
document.add(table);
PdfPTable tableCustomized = new PdfPTable(3);
PdfPTable tableNested = new PdfPTable(6);
Phrase type = new Phrase("Type :",fontLibelle);
Phrase surface = new Phrase("Surface :",fontLibelle);
Phrase venteloc = new Phrase("Vente / Location :",fontLibelle);
tableNested.addCell(type);
tableNested.addCell(surface);
tableNested.addCell(venteloc);
tableNested.addCell("Bureau");
tableNested.addCell("5000 m²");
tableNested.addCell("Vente");
tableCustomized.setTotalWidth(40);
PdfPCell cellule = new PdfPCell(new Paragraph("test table",fontTitre));
cellule.setBackgroundColor(new Color(0,255,255));
cellule.setBorder(0);
tableCustomized.addCell(cellule);
tableCustomized.addCell("fffff");
tableCustomized.addCell("ddedede");
tableCustomized.addCell("jryjyjtjtyjtjty");
tableCustomized.addCell("ffhyjjuj");
tableCustomized.addCell(tableNested);
document.add(tableCustomized);
PdfPTable tableDyn = new PdfPTable(2);
tableDyn.setWidthPercentage(50);
//tableDyn.setHorizontalAlignment(80);
//tableDyn.setTotalWidth(25);
try {
String strClassName = driver;
Class.forName(strClassName);
Connection conn = DriverManager.getConnection(dbURL);
Statement stmt = conn.createStatement();
String sql = "SELECT top 10 * FROM TRANSACT";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
Phrase numTrans = new Phrase(rs.getString("NUM_TRANSACTION"),fontLibelle);
PdfPCell celluleNumTrans = new PdfPCell(new Paragraph(numTrans));
celluleNumTrans.setBackgroundColor(new Color(255,0,0));
celluleNumTrans.setBorder(0);
tableDyn.addCell(celluleNumTrans);
Phrase localCommune = new Phrase(rs.getString("LOCAL_COMMUNE"),fontLibelle);
tableDyn.addCell(localCommune);
}
rs.close();
stmt.close();
conn.close();
} catch(ClassNotFoundException e) {
System.err.println("Driver non chargé !");
e.printStackTrace();
} catch(SQLException e) {
System.err.println("erreur sql !");
e.printStackTrace();
}
document.add(tableDyn);
document.close();
response.setContentType("application/pdf");
response.setContentLength(baos.size());
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
} catch(DocumentException de){
System.out.println("Pb PDF : "+de);
}
} |
Partager