Viewing PDF File Using Java

How to call the pdf file out using java program?


Here is a sample program:

Java Code:

    public static void main(String args[])       //main function
        try                                      //try statement
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "c:\\chart.pdf");   //open the file chart.pdf

        } catch (Exception e)                    //catch any exceptions here
              System.out.println("Error" + e );  //print the error


Using PDFBox to read the text from the PDF file:

Java Code:

I re-tested the code, and is working fine. Please check out what I have written:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

public class PdfBoxTest
public static void main(String args[])
PDDocument pddDocument=PDDocument.load(new File("a.pdf"));
PDFTextStripper textStripper=new PDFTextStripper();
System.out.println(textStripper.getText(pddDocumen t));
catch(Exception ex)

In the classpath, I have included the following jar files: 

1) pdfbox-1.2.1.jar
2) fontbox-1.2.1.jar
3) commons-logging-1.1.1.jar


I am not sure about the latest version of pdfbox, I guess it should be 1.2.1. 

If you are getting error, it is probably the jar file not being set in the classpath, as the exception is purely related to inability to find the class file, it means the jar file is not being located at that location. Rest of the things will go fine if your jar file is properly recognized. 

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.