But sometimes it may be that the Acrobat executable is not in this path, the question is possible to run Acrobat by without having to indicate all its path in Java?
Will the file have to be opened exclusively with Acrobat? if not, you can open the file with the program associated with that type of file with the method Desktop#open
:
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("c:\\teste1.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// Não há programas instalados para abrir esse arquivo
}
}
If it is necessary to pass arguments to the file, the method Desktop#open
may not serve in this case.
What you will have to do is search for the file type of the extension with the command assoc
and use the command ftype
to get the program associated with file type.
In that reply from Soen shows how to implement this:
public static void openPdfWithParams(File pdfFile, String params){
try {
// encontra o tipo de arquivo da extensão pdf
Process p = Runtime.getRuntime().exec("cmd /c assoc .pdf");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
String type = line.substring(line.indexOf("=")+1);
reader.close();
// encontra o executável associado ao tipo de arquivo
p = Runtime.getRuntime().exec("cmd /c ftype " + type);
p.waitFor();
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
line = reader.readLine();
reader.close();
String exec = line.substring(line.indexOf("=") + 1);
// Substitui "%1" parâmetros e o caminho de arquivo
String commandParams = String.format("/A \"%s\" \"%s\"", params ,pdfFile.getAbsolutePath());
String command = exec.replace("\"%1\"", commandParams);
p = Runtime.getRuntime().exec(command);
} catch (Exception e) {
System.out.println("Erro ao tentar abrir o arquivo PDF");
e.printStackTrace();
}
}
To use, do so:
public static void main (String[] args) throws java.lang.Exception
{
openPdfWithParams(new File("C:\\teste1.pdf"), "page=5&zoom=150");
}
The line above will try to open the file C: teste1.pdf on the page 5 with zoom of 150 with the associated program to open files .pdf
.
Sorry, I posted an answer but I ended up not asking if you intend to only open Acrobat or would like to open any file in pdf.
– viana
It is that as I need to open certain pages I need to pass parameters as page is the way
– R.Santos
But what is the problem of the way?! Usually when installing Acrobat Reader always the way is the same. Unless it is windows 32 or 64bits. Then you can make a condition by checking.
– viana