tags:

views:

42

answers:

2

Hi,

I want to show a PDF document from a Java (Swing) application in a system independent manner (provided that a PDF viewer is properly installed on the target system).

Also I'd like to deploy this PDF document using Java WebStart.

Could you please tell me the "standard" way to achieve this? (I confess, I'm to lazy/busy to look up the details ...) Thanks!

+1  A: 

You can use the Java 6 Desktop system and Desktop.open() to open the associated desktop application for your document (in this case, a PDF file).

Brian Agnew
Exactly what I was looking for for the first part of the question! Thanks!
MartinStettner
+1  A: 

I assume you mean you want to deploy the PDF along with the Java Web Start application? If so, you simply need to package the PDF(s) with your webstart application. When your application downloads and runs, the PDFs will have come too, and your code can use the getClass().getResource("/where/is/my.pdf") type of lookup to locate the PDF and then operate on it for display. You might also need to get your code to read the PDF out of the resources and save it in a temp file (File.createTempFile()) so that the PDF viewer can see it.

rough idea:

  // Find the PDF in the Webstart App download
  InputStream in = getClass().getResourceAsStream("/where/is/my.pdf");

  // create a temp file to copy the pdf to
  File tmpFile = File.createTempFile("my", "pdf");
  OutputStream out = new FileOutputStream(tmpFile);

  // stream the file from in to out ... heaps of examples on the net for doing this ("copy files")

  // display the file
  Desktop.getDesktop().open(tmpFile);

  // ideally clean the tmp file up at some point.
jowierun
Thanks. But in this case, cleaning the temp file is exactly the tricky part since I do not know, when the user will close the external viewer. But I will go this way at first as long as I do not find a way to deploy the PDF file directly (i.e. not as resource)
MartinStettner
I wouldn't be too concerned about leaving the temp files around on client machines on a per-client-request basis. If you look at IE behaviour when it downloads a PDF it leaves it in temp. If your platforms is not windows, you can likely delete the file even though it's being viewed (it will be released later). On windows you could try deleting the file at some time later if your application hangs around for a while (such as when application terminates - see File.deleteOnExit() too).
jowierun