1

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!

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

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)
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).
1

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).

1 Comment

Exactly what I was looking for for the first part of the question! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.