Solved

Diaplay PDF file in JAVA

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

Thanking you
0
Comment
Question by:sree032397
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

789 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