Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open PDF file on the fly from a Java application

Tags:

java

pdf

Is there any way to have a code that opens a PDF file in Java application in a platform independant way? I mean using a batch file in Windows could do that. Is there any other way to have a platform independent code to open PDF files on the fly?

like image 535
Mr CooL Avatar asked Mar 30 '10 16:03

Mr CooL


2 Answers

I'd try Desktop.open(File), which:

Launches the associated application to open the file.

So this code should do the trick:

if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File("/path/to/file.pdf");
        Desktop.getDesktop().open(myFile);
    } catch (IOException ex) {
        // no application registered for PDFs
    }
}
like image 96
Michael Myers Avatar answered Oct 16 '22 23:10

Michael Myers


You can use Runtime to execute and script and there are also several Java PDF viewers out there (ie Icepdf, JPedal, PDFRenderer).

like image 3
mark stephens Avatar answered Oct 17 '22 00:10

mark stephens