voila je me demander si c'était possible d'ouvrir un fichier pdf en java.
jappuie sur un bouton auquel est associé une action et on ouvre le fichier pdf avec acrobat, xpdf, ... nimporte
merci davance joneil
voila je me demander si c'était possible d'ouvrir un fichier pdf en java.
jappuie sur un bouton auquel est associé une action et on ouvre le fichier pdf avec acrobat, xpdf, ... nimporte
merci davance joneil
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 public class BrowserControl { public void displayURL(String url) { boolean windows = isWindowsPlatform(); String cmd = null; try { if (windows) { cmd = WIN_PATH + " " + WIN_FLAG + " " + url; Process p = Runtime.getRuntime().exec(cmd); } else { cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")"; Process p = Runtime.getRuntime().exec(cmd); try { int exitCode = p.waitFor(); if (exitCode != 0) { cmd = UNIX_PATH + " " + url; p = Runtime.getRuntime().exec(cmd); } } catch(InterruptedException x) { System.err.println("Error bringing up browser, cmd='" + cmd + "'"); System.err.println("Caught: " + x); } } } catch(IOException x) { System.err.println("Could not invoke browser, command=" + cmd); System.err.println("Caught: " + x); } } public boolean isWindowsPlatform() { String os = System.getProperty("os.name"); if ( os != null && os.startsWith(WIN_ID)) return true; else return false; } private static final String WIN_ID = "Windows"; private static final String WIN_PATH = "rundll32"; private static final String WIN_FLAG = "url.dll,FileProtocolHandler"; private static final String UNIX_PATH = "netscape"; private static final String UNIX_FLAG = "-remote openURL"; }cette classe permet de gerer l ouverture d'un fichier avec le programme par défaut donc tout type de fichier devrait marcher.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 BrowserControl bc = new BrowserControl(); File f = new File("chemin_du_fichier_a_ouvrir.extension"); bc.displayURL("file://" + f.getAbsolutePath());
Partager