How to open a PDF file in Java
Published: January 12, 2010 , Updated: April 14, 2011 , Author: mkyong
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(); } } }
Reference
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
Very usefull, i want to to the same but using C++ any help?
Hi All,
I want to read the byte stream from the web service and display the same as PDF to the user. Can someone please help me on that.
I have tried various examples in google, it is working fine in IE, but somehow in Mozilla and chrome, for PDF’s greater than 15 kb, it is printing the byte response directly on the browser window.
Any help will be really appreciated.
Hello,
It appears that the first method allows you to wait for the process to complete whereas the second creates a child process and leaves it alone. In the second method, you lose some control. Is there an easy way for the program to wait for desktop.open in a in a thread in the second case ?
thanks
hey mkyong,
i want to open a an excel-file when i click on a JPopUpMenu
how to do?
How can I open a pdf file with parameters?
For example: “..\\test.pdf” “page=3″
Thanks,
Tim
You may need to try this iText jar library.
This is very useful for me.
Very useful… Thanks a millions…
ThanX, It was very useful
Thank You Very much Working very nice
Working Very Nice Thanks Alot
Thank you sir!!!
THANK U SIR, UR CODE HELPED US LOT……..
Sir wonderful examples are given.. It is so helpful keep on posting
Hi Mkyong,
How to open a excel file in Java?Thank You.
Above solution should open excel file no problem.
However, to manipulate the excel file, you may need third party library like Apache POI.
http://poi.apache.org/
http://www.mkyong.com/spring-mvc/spring-mvc-export-data-to-excel-file-via-abstractexcelview/
thanks a lot we use this codes you help us to much
Simply does not work. Pdf file never opens on Desktop.
It’s working fine in my pc and many others. Do you have any error message?
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 very much mate, you are a life saver!
Maxxxxxx…………
Thanks for sharing this. I was looking for this. Very well explained.