Asked By
thomas
10 points
N/A
Posted on - 11/05/2012
Hi,
How to open PDF using java code? I would like to know about opening PDF form in java created systems. Is there a way you can share or illustrate with me the proper coding technique? Tutorials will also help me a lot along the way.
Regards,
Thomas.
How to open PDF using java code?
Hi Thomas,
I have a code to open a PDF file with java. I had learned about this code from my Java Tutor. This is very easy to learn and understand. Here is in windows, we know that there is a file named as "rundll32". This is a DOS command which is also used to launch or open a PDF file.
Code:
package com.joy.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:\JavaCode.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();
}
}
}
Code Explanation: Simply this code finds a PDF File at a path C:\JavaCode.pdf. If file exists then it will be opened otherwise you will get message as "File is not exists".
Thank You.
How to open PDF using java code?
Hi Thomas,
Here are the methods you can use you need to follow.
> By using “rundll31” – this is the Windows Platform Solution
*For Windows, you can use "rundll31" command in order to open PDF file,
> By using “Awt Desktop” – Cross Platform Solution
*This Solution Awt Desktop Cross Platform Solution is much recommended. This works in windows and Mac Platforms.
Hope this will help.
How to open PDF using java code?
Hi Thomas,
1. To open PDF using java code we suggest a syntax:
https://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html
And 1+ for none-window-only solution. – james.garriss
2. You can use this method which has made that Windows infiltration obsolete:
Desktop. getDesktop() . open (new File("C :/C.pdf"();
NOTE: Put the file name inside double quotes to work around the space problem.