Is it possible to run Acrobat Reader without putting all the way of its executable?

Asked

Viewed 305 times

6

I am opening files in PDF format by Java and for this I am running Acrobat by Java also, follows the path line to the Acrobat executable:

String cmd = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe";

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?

The complete code is this:

String cmd = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe";

String param1 = "/A";
String param2 = "page=" + Integer.toString(i) + "&zoom=150";

String[] cmds = new String[]{cmd, param1, param2, pdfFile.getAbsolutePath()};
Process p = new ProcessBuilder(cmds).start();
  • 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.

  • It is that as I need to open certain pages I need to pass parameters as page is the way

  • 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.

2 answers

5


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.

  • Yeah, it’s just that if I put it that way he’s screwed up.

  • I did so and continues to open the pdf on page 01 always

  • @R.Santos I edited the answer, tested it here and it worked, see if it works for you.

  • Perfect @zekk, that’s what I needed, thank you very much.

1

You would have another alternative to open a file .pdf, but so:

public static void main(String args[]) {
    try {
        // o arquivo myfile.pdf sera aberto assim
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "c:\\myfile.pdf");

    } catch (Exception e) {
        System.out.println("Error" + e); //imprime o erro
    }
}
  • Can you tell if you have arguments to the via command getRuntime().exec?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.