打开带参数的PDF文件

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

我尝试了两件不同的事情。

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

有没有办法用“page = 4”之类的参数启动PDF阅读器直接跳到右页?

提前致谢

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

找到后,您只需按照acrobat的文档中的预期构建命令行:

  /A "=" "" 

我带来了以下解决方案:

 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。