简体   繁体   中英

open PDF File with parameters

i am working on a java based tool, which should search for PDF files on selected directories and which should search for special words/sentences in this PDF files. After that a JList shows the files which fits and with a double-click on one of these entries the PDF Reader (Adobe Reader) should open this file directly on the page where the word/sentence appeares.

I tried two different things.

Runtime.exec:

try{
    Runtime.getRuntime().exec("rundll32" + " " + "url.dll,FileProtocolHandler /A page=4" + " " + o.toString()); 
}catch(IOException ex) { 
    ex.printStackTrace(); 
}

Desktop open:

if(Desktop.isDesktopSupported()) {
    try{
        Desktop d = Desktop.getDesktop();
        d.open(new File(o.toString()));
    }catch(IOException ex) {
        ex.printStackTrace();
    }
}

Is there a way to start the PDF Reader with parameters like "page=4" to jump directly to the right page?

Thanks in advance

One of the problem you might face is not being able to directly call acrobat, if not in the Path of the computer. The solution uses two Windows commands : assoc and ftype to retrieve acrobat executable path.

Once found, you just have to build the command line as expected in acrobat's documentation :

<Acrobat path> /A "<parameter>=<value>" "<PDF path>"

I came with the following solution:

public void openPdfWithParams(File pdfFile, String params){

    try {
        //find the file type of the pdf extension using assoc command
        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); //keeping only the file type
        reader.close();

        //find the executable associated with the file type usng ftype command
        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); //keeping only the command line


        //building and executing the final command
        //the command line ends with "%1" to pass parameters - replacing it by our parameters and file path
        String commandParams= String.format("/A \"%s\" \"%s\"", params ,pdfFile.getAbsolutePath()); 
        String command = exec.replace("\"%1\"", commandParams);
        p = Runtime.getRuntime().exec(command);


    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error while trying to open PDF file",e );
        e.printStackTrace();
    }
}

Beware, the code is highly optimistic for the sake of readability, several tests has to be made to ensure that the commands return the expected result. Additionally, this code has a Java6 syntax that could certainly benefit an upgrade to Java7's "try with resources" and nio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM