How to open a PDF file in Java
In this article, we show you two ways to open a PDF file with Java.
1. rundll32 – Windows Platform Solution
In Windows, you can use “rundll32” command to launch a PDF file, see example :
package com.mkyong.jdbc;
import java.io.File;
//Windows solution to view a PDF file
public class WindowsPlatformAppPDF {
public static void main(String[] args) {
try {
if ((new File("c:\\Java-Interview.pdf")).exists()) {
Process p = Runtime
.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler c:\\Java-Interview.pdf");
p.waitFor();
} else {
System.out.println("File is not exists");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
2. Awt Desktop – Cross Platform Solution
This Awt Desktop cross platform solution is always recommended, as it works in *nix, Windows and Mac platforms.
package com.mkyong.io;
import java.awt.Desktop;
import java.io.File;
//Cross platform solution to view a PDF file
public class AnyPlatformAppPDF {
public static void main(String[] args) {
try {
File pdfFile = new File("c:\\Java-Interview.pdf");
if (pdfFile.exists()) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
} else {
System.out.println("Awt Desktop is not supported!");
}
} else {
System.out.println("File is not exists!");
}
System.out.println("Done");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Thank you!! this code helped me so much…
Nice, an addition could be to check if the file exists. This can be done with the following statement:
if( (new File(“c:/Java-Interview.pdf”)).exists() )
Thank you Thank you I can’t thank you enough had this error for a week now
this program is useful if the default pdf viewer is chrome…. but in my case i want to open pdf viewer as chrome even though chrome it’s not a default pdf viewer…. how can i get that…
When I use this in a JavaFX application to open a .pdf file on Ubuntu Linux the application hangs. Any idea what the problem might be, or if there is a work-around?
Thanks.
this worked for me:
https://stackoverflow.com/questions/23176624/javafx-freeze-on-desktop-openfile-desktop-browseuri
How to read pdf file inside in jframe using jpanel, jtextarea, jlable, etc…
Hey, do you known how to use Desktop.getDesktop().open(pdfFile); to open pdf file in full screen?
Dear sir
How can i find if the file is being opened in the runtime from any directory
My question is little different from the context . I need advice from the respected mentors.
How to open a pdf or any external program in a new process using your cross platform solution?
Hi ,
Is it possible to open the pdf in adobe reader pro via java ?
hi mkyong, actually i want to display pdf file in browser or in Jtable on button click but my pdf file is stored in db. so ho to do this..
Works well Thanks…!!!
its works
How to open a pdf file from menuconfig.xml in struts2 application.Can somebody please help me out.
Thanks my friend. Has solved my doubt!
how to open dynamically generated pdf file(stored in server) in browser????
IT’s Working very well. thanks
sir
Runtime.getRuntime().exec(C:\\Documents and Settings\\car\\samples)
In The Above Command Space Between The Documents,and,Settings Are showing The Error
What Can I Do For It……………
I use Aspose.PDF for Java to read and create my pdf files and they also provide java code also for developers to use in their API. here is the link:
http://www.aspose.com/java/pdf-component.aspx
Good one mate! Very helpful…
I want to close the pdf opened using option-1.
I tried p.destroy() but it closes the process but not the pdf.
dear mykoung
can you pls post a generic way of opening any file in windows and unix system
for example i will have c://test/test/pdf
but in unix i will under dir test/tmp/files
Pleae help
If you want to obtain much from this paragraph then you have to apply such strategies to your won webpage.
This is very interesting, You’re a very skilled blogger. I’ve joined your rss
feed and look forward to seeking more of your magnificent post.
Also, I’ve shared your website in my social networks!
How can i open pdf in solaris machine/server
I have work example fine but when i have use this example with tomcat than not working.
Steps:
1) Prepare one web project
2) Write your sample code and put pdf file on webapps folder.
This example working fine when i test in local but when i use this in network that time this is working but some issue like i have upload project in one machine and run from other machine that time file is open in uploaded machine not client.
I want to know how can i open pdf(Which is located at server or any URL) file at client side.
I am now not certain the place you’re getting your information, however good topic. I needs to spend some time studying more or working out more. Thank you for excellent info I was in search of this information for my mission.
Love reading ur articles.. very simple and fantastics.. Good work!
thanks a lot! this is the thing that I searched for my project! 😀
Hello again, I have got it working. I want to run a .bat file to open Apache Tomcat server. On Windows PC the command prompt must stay open after the file is run for the server to stay running.For the code you have given the command prompt closes after running the batch file.Any suggestions on how I can modify the code?