Link to home
Start Free TrialLog in
Avatar of sree032397
sree032397

asked on

Diaplay PDF file in JAVA

How can I Display a PDF File in JAVA

Thanking you
Avatar of mikal_sk
mikal_sk

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";
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
this article illustrates dat how to open a non HTML document from a servlet

http://www.javaworld.com/javaworld/javatips/jw-javatip94.html
Avatar of sree032397

ASKER

No, I dont want to open it in External Application,
It should be displayed in a Java UI
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/

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
I want to display it using Swing
ASKER CERTIFIED SOLUTION
Avatar of mikal_sk
mikal_sk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

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
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.
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