How to open PDF using java code?

Asked By 10 points N/A Posted on -
qa-featured

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.

SHARE
Best Answer by JOY Schilling
Best Answer
Best Answer
Answered By 5 points N/A #86548

How to open PDF using java code?

qa-featured

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.
Answered By 0 points N/A #86549

How to open PDF using java code?

qa-featured

Is there any way to open a PDF file on a Java application that will work independently of the platform on which it is used?

Batch File in Windows is able to perform this task, but I am looking for a way to open PDF file irrespective of platform.

Here is a link to website to open a PDF using Java code.

 
 
Answered By 0 points N/A #86553

How to open PDF using java code?

qa-featured

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.

Answered By 0 points N/A #86555

How to open PDF using java code?

qa-featured

Hey Thomas,

  • You don't need to go through any big tutorials for doing this simple thing.
  • Desktop open(File) is function which launches the associated application to open the file. It's present in package java.awt.Desktop.
  • Try the code in the file attached.
  • The code performs simple function of opening a file using it's by default application.

Hope this works for you.

Answered By 0 points N/A #86551

How to open PDF using java code?

qa-featured

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.

Related Questions