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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| package com.example.test;
import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class Facture extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
String produit,quantite,prix;
private JTable table;
public Facture() {
getContentPane().setLayout(new BorderLayout());
createTable();
}
public void createTable() {
try {
String quantitef;
String produitf;
String prixf;
String col[] = {"Produit", "Quantité", "Prix unitaire"};
String cont[][] = new String[10][3];
for(int i = 0; i < ValidationCommande.listproduit.size() ; i++) {
quantitef = ValidationCommande.listproduit.get(i).get ("quantite");
produitf = ValidationCommande.listproduit.get(i).get ("produit");
prixf = ValidationCommande.listproduit.get(i).get ("prix");
cont[i][0] = produitf;
cont[i][1] = quantitef + "";
cont[i][2] = prixf + "";
}
table = new JTable(cont, col);
JPanel tPanel = new JPanel(new BorderLayout());
tPanel.add(table.getTableHeader(), BorderLayout.NORTH);
tPanel.add(table, BorderLayout.CENTER);
getContentPane().add(tPanel, BorderLayout.CENTER);
} catch (Exception e) {
}
}
public void genererPdf(String Nom, String Prenom, String Adresse, String Ville, String code_postal, String Email, String numCO, String date_commande, String Produit, String quantite, String prix, String prix_total) {
Document doc = new Document(PageSize.A4);
try {
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/PDF";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
Log.d("PDFCreator", "PDF Path: " + path);
File file = new File(dir, "Facture_" + (numCO +1)+".pdf");
FileOutputStream fOut = new FileOutputStream(file);
PdfWriter writer = PdfWriter.getInstance(doc, fOut);
//logo de l'entreprise
/*Image img = Image.getInstance("C:/Users/Guillaume/Desktop/logo.jpg");
img.scalePercent(20);
img.setAlignment(Element.ALIGN_LEFT);
doc.add(img);*/
//coordonnées de l'entreprise
Paragraph p = new Paragraph("BuyMe");
p.setAlignment(Element.ALIGN_LEFT);
Paragraph p1 = new Paragraph("10 rue de Rousseau");
p1.setAlignment(Element.ALIGN_LEFT);
Paragraph p2 = new Paragraph("75001 Paris");
p2.setAlignment(Element.ALIGN_LEFT);
Paragraph p3 = new Paragraph("contact@BuyMe.com");
p3.setAlignment(Element.ALIGN_LEFT);
p3.setSpacingAfter(30);
doc.add(p);
doc.add(p1);
doc.add(p2);
doc.add(p3);
//date de la commande
Paragraph p4 = new Paragraph("Date de la commande : " + date_commande);
p4.setSpacingAfter(10);
p4.setAlignment(Element.ALIGN_LEFT);
//coordonnées du client
Paragraph p5 = new Paragraph(Nom + " " + Prenom);
p5.setAlignment(Element.ALIGN_RIGHT);
Paragraph p6 = new Paragraph(Adresse + " \n " + Ville + " " + code_postal);
p6.setAlignment(Element.ALIGN_RIGHT);
Paragraph p7 = new Paragraph(Email);
p7.setAlignment(Element.ALIGN_RIGHT);
p7.setSpacingAfter(15);
Paragraph p8 = new Paragraph("Vous avez commandé : ");
p8.setAlignment(Element.ALIGN_LEFT);
Paragraph p9 = new Paragraph ("Pour un prix total de : " +prix_total+ " ");
p9.setAlignment(Element.ALIGN_CENTER);
p9.setSpacingBefore(10);
Paragraph p10 = new Paragraph ("Vous trouverez ci-dessous le détail de votre commande :");
p10.setAlignment(Element.ALIGN_LEFT);
p10.setSpacingBefore(10);
doc.add(p5);
doc.add(p6);
doc.add(p7);
doc.add(p4);
doc.add(p8);
//titre et numéro de commande
Paragraph p11 = new Paragraph("Facture n° " + numCO);
p11.setAlignment(Element.ALIGN_CENTER);
p11.setSpacingBefore(20);
p11.setSpacingAfter(30);
//création du tableau
PdfPTable table = new PdfPTable(4);
PdfPCell cell1 = new PdfPCell(new Paragraph("Produit"));
cell1.setGrayFill(0.8f);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph("Prix unitaire ()"));
cell1.setGrayFill(0.8f);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph("Quantité"));
cell1.setGrayFill(0.8f);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph("Prix total ()"));
cell1.setGrayFill(0.8f);
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell1);
for(int i = 0; i < ValidationCommande.listproduit.size() ; i++) {
String quantiteF = ValidationCommande.listproduit.get(i).get ("quantite");
String produit = ValidationCommande.listproduit.get(i).get ("produit");
Paragraph p12 = new Paragraph(" "+ quantiteF +" "+produit);
p12.setAlignment(Element.ALIGN_LEFT);
doc.add(p12);
int t = Integer.parseInt(ValidationCommande.listproduit.get(i).get ("prix")) * Integer.parseInt(ValidationCommande.listproduit.get(i).get ("quantite"));
String total = String.valueOf(t);
String prix_unité = ValidationCommande.listproduit.get(i).get ("prix");
String quantity = ValidationCommande.listproduit.get(i).get ("quantite");
String Produitt = ValidationCommande.listproduit.get(i).get ("produit");
PdfPCell cell2 = new PdfPCell(new Paragraph(prix_unité));
cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
PdfPCell cell3 = new PdfPCell(new Paragraph(quantity));
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
PdfPCell cell4 = new PdfPCell(new Paragraph(total));
cell4.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(Produitt);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
}
doc.add(p10);
doc.add(p11);
doc.add(table);
doc.add(p9);
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
Graphics2D g2 = cb.createGraphicsShapes(500, 500);
Shape oldClip = g2.getClip();
g2.clipRect(0, 0, 500, 500);
//table.print(g2);
g2.setClip(oldClip);
g2.dispose();
cb.restoreState();
doc.close();
} catch (Exception e) {
System.out.println(e);
}
}
} |