打开带参数的PDF文件 [英] open PDF File with parameters

查看:285
本文介绍了打开带参数的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基于java的工具,它应该搜索所选目录上的PDF文件,并且应该搜索此PDF文件中的特殊单词/句子。之后,JList显示适合的文件,双击其中一个条目,PDF Reader(Adobe Reader)应直接在出现单词/句子的页面上打开此文件。

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:

Runtime.exec:

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

桌面打开:

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

有没有办法用参数启动PDF阅读器喜欢page = 4直接跳到右边页面?

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

提前致谢

推荐答案

您可能遇到的一个问题是,如果不是在计算机的路径中,则无法直接调用acrobat。该解决方案使用两个Windows命令: assoc和ftype 以检索acrobat可执行文件路径。

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.

一旦找到,你只需按照预期在acrobat的文档中构建命令行:

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();
    }
}

请注意,代码是非常乐观的可读性,必须进行多次测试以确保命令返回预期结果。此外,此代码具有Java6语法,这肯定有利于升级到Java7的尝试使用资源和nio。

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.

这篇关于打开带参数的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆