Salut
Je cherche comment puis je ecrire dans un fichier word ou pdf l'un des deux j'ai fait des recherches j'ai trouvé que la meilleur manier de le faire c'est de travailler avec la bibliothèque RTF ou IText ...
J'ai télécharger ces bibliotheque je les ai fait integrer dans mon projet ca m'a donnée erreur dans le code et meme dans les paquages
Voici le code que j'ai télécharger du net
Et merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 /** * */ import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import com.lowagie.text.*; import com.lowagie.text.rtf.RtfWriter2; /** * @author Lopez Thomas * */ public class main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String[] data=null; String fileo = "test.txt"; try{ fileo = args[0]; }catch(Exception e){ System.out.println("pas de fichier de configuration"); fileo = "test.txt"; } data = readFile(fileo); System.out.println(makepdf(data,"test")); } private static String makepdf(String[] datapdf,String file) { // TODO Auto-generated method stub String output="generation reussie"; Document document = new Document(); try { RtfWriter2.getInstance(document, new FileOutputStream(file+".doc")); document.open(); int i=0; /*changer la font * RtfFont rtfFont = new RtfFont("Comic Sans MS"); * Paragraph para = new Paragraph("This is a paragraph in Comic Sans MS", rtfFont); * */ for(i=0;i<(datapdf.length );i++){ document.add(new Paragraph(datapdf[i]));} } catch (DocumentException de) { output = "generation pas reussie " ; System.err.println(de.getMessage()); } catch (IOException ioe) { output = "generation pas reussie exception" ; System.err.println(ioe.getMessage()); } document.close(); return output; } public static String[] readFile(String f) { String[] data = null; File ft= new File("C:/path/to/",f); if (ft.exists()) { System.out.println("Ouverture du fichier"); }else{ System.out.println("fichier n'exite pas"); } int taille = linesInFile(ft); System.out.println(taille); data = new String[taille];// try { BufferedReader in = new BufferedReader(new FileReader(f)); String str; int i=0; while ((str = in.readLine()) != null) { data[i] = str; System.out.println(data[i]); i++; } in.close(); } catch (IOException e) { data[0] = "erreur"; return data; } return data; } private static int linesInFile( File file ) { int cpt = 0; try { String fic = file.getName().toLowerCase(); if (file.isFile()) { BufferedReader bfr = new BufferedReader( new FileReader( file ) ); while( bfr.readLine() != null ) { cpt++; } } } catch (Exception e) { System.out.println( "linesInFile eror :" + e ); } return cpt; } }
Partager