2

I'm trying to open a PDF file with Java and jump to a specific page. Here is my code to open a PDF file:

if (Desktop.isDesktopSupported()) {
  try {
    File file = new File("file.pdf");
    Desktop.getDesktop().open(file);
  } catch (IOException ex) {
    System.err.println('error:' + ex);
  }
}

Can someone help me ?

Rene Knop
  • 1,788
  • 3
  • 15
  • 27
Azrix
  • 99
  • 1
  • 10
  • Did you check if using open parameters helps: https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf (I'm posting this as a comment, not as an answer because I didn't check myself. However, the link may be helpful.) – Bruno Lowagie Sep 19 '18 at 13:23
  • I think pdf files should be associated with one of the programs in your PC which could be used for opening these files – Dumbo Sep 19 '18 at 13:25
  • Otherwise you could use `Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);`. It is not so pretty but it should do what you need – Dumbo Sep 19 '18 at 13:30
  • Yes I think I may be helpful for Adobe Acrobat Reader, but I would like it to work on all PDF reader (well, not all but at least Chrome and Firefox too) – Azrix Sep 19 '18 at 13:30
  • Probably you have to run the reader with an argument like here https://stackoverflow.com/questions/619158/adobe-reader-command-line-reference – PeterMmm Sep 19 '18 at 13:30
  • I'm gonna try @Dumbo, thank you! – Azrix Sep 19 '18 at 13:31
  • Well, I didn't see this psot @PeterMmm, thank you! – Azrix Sep 19 '18 at 13:33
  • I think you shouldn't use Adobe Reader, because not all of the computer may have it installed – Dumbo Sep 19 '18 at 13:35

1 Answers1

0

In case you are using specific program to open PDF it is possible to use appropriate command line arguments. E.g. if you are using Acrobat Reader you can do so:

AcroRd32.exe /A "page=<PAGE_NUMBER>" <PATH_TO_PDF>

In Java it might look something like this:

Runtime.getRuntime().exec("AcroRd32.exe /A \"page=10\" C:/test.pdf");

Of course AcroRd32.exe should be in the PATH in this case, otherwise you will need to provide full path to the program.

marme1ad
  • 1,253
  • 8
  • 8