Solved

Diaplay PDF file in JAVA

Posted on 2006-06-27
16
809 Views
Last Modified: 2013-11-23
How can I Display a PDF File in JAVA

Thanking you
0
Comment
Question by:sree032397
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
16 Comments
 
LVL 1

Expert Comment

by:mikal_sk
ID: 16990624
Hey

Try this : hardcode youre path into displayURL method : "file://".



import java.io.IOException;

/*
* A simple, static class to display a URL in the system browser.
*/

public class BrowserControl{

    /**
     * Display a file in the system browser.  If you want to display a
     * file, you must include the absolute path name.
     *
     * @param url the file's url (the url must start with either "http://"or
     * "file://").
     */
  public static void displayURL(String url) {

        boolean windows = isWindowsPlatform();
        String cmd = null;

        try{
            if (windows){

                // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
                cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
                Process p = Runtime.getRuntime().exec(cmd);
            }
            else{
                // Under Unix, Netscape has to be running for the "-remote"
                // command to work.  So, we try sending the command and
                // check for an exit value.  If the exit command is 0,
                // it worked, otherwise we need to start the browser.
                // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
                cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
                Process p = Runtime.getRuntime().exec(cmd);
                try{
                    // wait for exit code -- if it's 0, command worked,
                    // otherwise we need to start the browser up.
                    int exitCode = p.waitFor();
                    if (exitCode != 0){
                        // Command failed, start up the browser
                        // cmd = 'netscape http://www.javaworld.com'
                        cmd = UNIX_PATH + " "  + url;
                        p = Runtime.getRuntime().exec(cmd);
                    }
                }
                catch(InterruptedException x){
                    System.err.println("Error bringing up browser, cmd='" +cmd + "'");
                    System.err.println("Caught: " + x);
                }
            }
        }
        catch(IOException x){
            // couldn't exec browser
            System.err.println("Could not invoke browser, command=" + cmd);
            System.err.println("Caught: " + x);
        }
    }
    /**
     * Try to determine whether this application is running under Windows
     * or some other platform by examing the "os.name" property.
     *
     * @return true if this application is running under a Windows OS
     */
    public static boolean isWindowsPlatform(){

        String os = System.getProperty("os.name");
        if ( os != null && os.startsWith(WIN_ID))
            return true;
        else
            return false;
    }
    /**
     * Simple example.
     */
    public static void main(String[] args){
        displayURL("http://www.javaworld.com");
    }
    // Used to identify the windows platform.
    private static final String WIN_ID = "Windows";
    // The default system browser under windows.
    private static final String WIN_PATH = "rundll32";
    // The flag to display a url.
    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
    // The default browser under unix.
    private static final String UNIX_PATH = "netscape";
    // The flag to display a url.
    private static final String UNIX_FLAG = "-remote openURL";
0
 
LVL 1

Expert Comment

by:mikal_sk
ID: 16990709
Hi again ive just tested this solution, it works for me.

init the class like this

// Abselute path to file
 public static void main(String[] args){
        displayURL("C://JAVA//Courses//LANGUAGE//WJ2751.pdf");
    }

make sure you have adobe reader innstalled :)


BR
Mike
0
 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 16990750
this article illustrates dat how to open a non HTML document from a servlet

http://www.javaworld.com/javaworld/javatips/jw-javatip94.html
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sree032397
ID: 16990763
No, I dont want to open it in External Application,
It should be displayed in a Java UI
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 16990943
Try:

Runtime.exec("adobe.exe", new String[] { "paper1.pdf" });

or u can try this

Process theProcess =
Runtime.getRuntime().exec("/Program Files/Adobe/Acrobat7.0/Reader/AcroRd32.exe C:\\filename.pdf");

You need to pass the absolute pathway for the executable and the file being read.

if you are reading in a file from a file chooser you can also do the following

Process theProcess = Runtime.getRuntime().exec("/Program Files/Adobe/Acrobat7.0/Reader/AcroRd32.exe "+chooser.getSelectedFile().getPath());

hope this helps

Chk this site..they r saying some of their components r helpful for pdf display in java UI
http://sourceforge.net/projects/xom/
0
 
LVL 1

Expert Comment

by:mikal_sk
ID: 16991206

How do you want to display the PDF file : applet, Swing, Servlet

Also check out this tool, should also give you the opportunity to edit the documents:
http://www.lowagie.com/iText/

BR
Mike
0
 
LVL 1

Author Comment

by:sree032397
ID: 16991813
I want to display it using Swing
0
 
LVL 1

Accepted Solution

by:
mikal_sk earned 168 total points
ID: 16991958
Then you should be looking for a printing API:
http://schmidt.devlib.org/java/libraries-pdf.html

I would recommend taking a look at "Adobe Acrobat Reader for Java", since it is freeware.
You should find plenty of coding examples in each link.

I have not tried any of these myself, but I know iText is very popular.
http://www.lowagie.com/iText/

hope this helps
0
 
LVL 8

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 166 total points
ID: 16992461
0
 

Assisted Solution

by:ObiRenoKenobi
ObiRenoKenobi earned 166 total points
ID: 16998392
I also recommend to use iText. I've used it before to write and read PDF documents and works very well. I think that displaying it in a Swing component without an external API, is very difficult and time consuming... but yes, is possible.
0
 
LVL 1

Author Comment

by:sree032397
ID: 16998526

I Downloaded iText. But how to use iText for displaying PDF FIles in a swing component.

I downloaded acrobat.jar from ADobe Acrobat to display PDF Files. But when I tried to use it in a class file it giving an exception which I copied below :

Exception in thread "main" java.lang.NoClassDefFoundError: com/apple/mrj/MRJAbou
tHandler
        at PDFViewer.viewPDFDocument(PDFViewer.java:17)
        at PDFViewer.main(PDFViewer.java:69)
Press any key to continue...

Please help me in this regard
0
 

Expert Comment

by:ObiRenoKenobi
ID: 17003082
Well, you just make sure you have the API in your classpath before executing it, or if you're executing in another machine, make sure that the API is in the package you're distributing.
0
 
LVL 1

Expert Comment

by:mikal_sk
ID: 17007422
Had a closer look at PDF viewer it seems to be deprecated and havent really been maintained by Adobe.

Please have a look at this one :
http://www.jpedal.org

Looks good. You have to download a JAR file and follow the tutorials.

BR
Mike
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is about my first experience with programming Arduino.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question